
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Write a bash script to print a particular line from a file in C
In this program, we are given a file name text.txt. Our task is to print a particular line from the file.
For this there are multiple methods in bash script, they are awk, sed, head.
Syntax
$> awk ‘{if(NR==LINE_NUMBER) print $0}’ filename $> sed -n LINE_NUMBERp filename $head -n LineNumber filename | tail - n + LINE_NUMBER
Code to print a specific line in bash programming from file text.txt.
Using awk
$> awk ‘{if(NR==5) print $0}’ text.txt
Using sed
$>sed -n 5p text.txt
Using head
$head -n 5 filename | tail - n + 5
- Related Articles
- How to Create a New File in Linux from Bash?
- How to write into a file from command line using Python?
- Implement a Counter in Bash Script on Linux
- C program to remove a line from the file
- How to replace spaces in file names using bash script on Linux?
- Negate an if Condition in a Bash Script in Linux
- Write a C program to read a data from file and display
- Count lines in a file using Linux bash
- How to read a Specific Line From a File in Linux?
- How to write a single line in text file using Python?
- How to print a blank line in C#?
- Run a Function in a Script from the Command Line on Linux
- Print contents of a file in C
- How to check Syntax of a Bash Script without running it in Linux?
- When to Use an Alias vs Script vs a New Function in Bash

Advertisements