Omitting the First Line from Any Linux Command Output

Omitting the first line from any Linux command output

Pipe it to awk:

awk '{if(NR>1)print}'

or sed

sed -n '1!p'

Print a file, skipping the first X lines, in Bash

You'll need tail. Some examples:

$ tail great-big-file.log
< Last 10 lines of great-big-file.log >

If you really need to SKIP a particular number of "first" lines, use

$ tail -n +<N+1> <filename>
< filename, excluding first N lines. >

That is, if you want to skip N lines, you start printing line N+1. Example:

$ tail -n +11 /tmp/myfile
< /tmp/myfile, starting at line 11, or skipping the first 10 lines. >

If you want to just see the last so many lines, omit the "+":

$ tail -n <N> <filename>
< last N lines of file. >

how to make grep ignore first line and process other line

With sed:

sed '2,${/^#/d}' sample.txt

From second row (2) to last row ($): search (/.../) for rows beginning (^) with # and delete (d) them. Default action of sed is to print current row.

Output:


#"EVENT",VERSION, NAME
1,2,xyz
1,2,abc
1,2,asd
1,2,ert
1,2,xyz
1,2,abc
1,2,xyz

Get first line of a shell command's output

Yes, that is one way to get the first line of output from a command.

If the command outputs anything to standard error that you would like to capture in the same manner, you need to redirect the standard error of the command to the standard output stream:

utility 2>&1 | head -n 1

There are many other ways to capture the first line too, including sed 1q (quit after first line), sed -n 1p (only print first line, but read everything), awk 'FNR == 1' (only print first line, but again, read everything) etc.

bash - how to remove first 2 lines from output

The classic answer would use sed to delete lines 1 and 2:

sed 1,2d "$PGLIST"

How to skip a line every two lines starting by skipping the first line?

You have to invert your sed command: it should be n;p instead of p;n:

Your code:

for x in {1..20}; do echo $x ; done | sed -n 'p;n'
1
3
5
7
9
11
13
15
17
19

The version with sed inverted:

for x in {1..20}; do echo $x ; done | sed -n 'n;p'

Output:

2
4
6
8
10
12
14
16
18
20

Change output of only the first line in bash script

You generally don't want to run sed -i on the same file more than once.

Your entire task can be rephrased into just

sed -i '1s/TABLE/01 /' APGFPOLI.des.txt

If the replacement string should come from a shell variable, you need to use double quotes instead of single:

replacement="05"
sed -i "1s/TABLE/$replacement /" APGFPOLI.des.txt

If you want to keep your other tasks (which are not documented in the question) you can easily merge them into the same script. Remember, sed is a scripting language; you can feed in arbitrarily complex scripts (and some crazy people have even implemented desk calculators and Game of Life in sed scripts).

sed -i -E -e '1 s/TABLE/01 /' -e 's/CHAR/PIC X/' \
-e '/Numérique/s/;Numérique\s+([^;]*)/;PIC 9(\1)/' APGFPOLI.des.txt

If after this you want to pull out the result and display it with some decorations, I would add a second sed script without -i so that it displays the output on standard output without modifying the file.

sed '1s/\([^;]*\);\([^;]*\);\([^;]*\);.*/* \3\n05 \2 \1/;q'  APGFPOLI.des.txt

How to remove lines from the output of a command in a bash script

To print only the final line use tail:

ldapwhoami | tail -n 1

To delete the first three lines with sed, change your command to:

ldapwhoami | sed '1d;2d;3d;'

note the semicolons and the quotes

Also possible with awk

ldapwhoami | awk 'NR > 3'

The above assumes that all output goes to standard output. In unix though there are two output streams that are connected to each process, the standard output (denoted with 1 - that is used for the output of the program), and the standard error (denoted with 2 - that is used for any diagnostic/error messages). The reason for this separation is that it is often desirable not to "pollute" the output with diagnostic messages, if it is processed by another script.

So for commands that generate output on both steams, if we want to capture both, we redirect the standard error to standard output, using 2>&1) like this:

ldapwhoami 2>&1 | tail -n 1

(for awk and sed the same syntax is used)

In bash, the above may be written using shorthand form as

ldapwhoami |& tail -n 1

If all you need is the standard output, and you don't care about standard error, you can redirect it to /dev/null

ldapwhoami 2> /dev/null

Why does cut command skip first line in this while read line loop?

You are not using the variable $line set by read. Try instead

inputfile=$1
cat "$inputfile" | while read -r line
do
echo "$line" | cut -f2 >> results_file
done

In your original code, the while loop is actually run only once, not four times; try putting echo 'Hello!' in the loop to your original code. You would see the message only once, not four times. I guess, without echo "$line" | part, cut -f2 ... part consumes the pipe away.

That is, your while loop first consumes the first line of the stdin and puts this line in the variable $line, leaving the next three lines for later use. But $line is never used. Instead, the remaining three lines are consumed by the command cut.

All commands within a command group are within the scope of any redirections applied to a command group (or any compound command):

— https://mywiki.wooledge.org/BashGuide/CompoundCommands

The pipe operator creates a subshell environment for each command.

— https://mywiki.wooledge.org/BashGuide/InputAndOutput

We can interpret the quotes as "the stdin to your while loop (i.e., the output of cat "$inputfile") is accessed by cut, unless you sever its access by creating a new subshell e.g., by another pipe echo "$line" | ...."

By the way, you can just use cut -f2 "$inputfile" >> results_file without the while loop.



Related Topics



Leave a reply



Submit