How to tail all lines except first row [duplicate] - linux

This question already has answers here:
how to read file from line x to the end of a file in bash
(7 answers)
Closed 7 years ago.
For example, I have a file
1
2
3
then I want to output from 2nd row to tail
How can I do it in linux

tail -n+2 my_file
will output all the lines in myfile starting with line 2. (-n2 would show you the last two lines.)
tail has lots more options. Type man tail for complete documentation.

shorter with
$ sed 1d filename
or with awk
$ awk 'NR>1' filename

Related

Shell script make lines in one huge file into two seperate files in one go? [duplicate]

This question already has answers here:
How to save both matching and non-matching from grep
(3 answers)
Closed 1 year ago.
Currently My shell script iterate the lines in one huge file two times:
(What I want to do is just like the shell script below.)
grep 'some_text' huge_file.txt > lines_contains_a.txt
grep -v 'some_text' huge_file.txt > lines_not_contains_a.txt
but it is slow.
How to do the same thing only iterate the lines once?
Thanks!
With GNU awk:
awk '/some_text/ { print >> "lines_contains_a.txt" }
!/some_text/ { print >> "lines_not_contains_a.txt" }' huge_file.txt
With sed:
sed -n '/some_text/ w lines_contains_a.txt
/some_text/! w lines_not_contains_a.txt' huge_file.txt

How to Save 'specific' line from terminal output to file? [duplicate]

This question already has answers here:
Bash tool to get nth line from a file
(22 answers)
Closed 4 years ago.
I am currently using the following to save terminal outputs to file:
$command -someoptions >> output.txt
However, I am only interested in one line from the terminal output.
Is there a way to do this by changing the above expression. Or will I have to delete lines after the 'output.txt' file is formed?
For example: If my output is:
line 1
line 2
line 3
line 4
line 5
and all I want to save is:
line 4
where line 4 contains unknown information.
I am asking as I will later wish to script this command.
Many thanks,
Solution Found:
I ended up using:
$command -someoptions | sed -n '4p' >> output.txt
This is a classic simple grep issue.
$command -someoptions | grep 'line 4' >> output.txt
You could refine that with more pattern complexity, and might need it depending on how precisely you need to match the data.
Try with this command:
$command -someoptions | grep " filter " >> output.txt
filter must be replaced by an element that distinguishes your line 4 from the other lines.

insert underscore in columns of a text file shell [duplicate]

This question already has answers here:
Replace whitespace with a comma in a text file in Linux
(9 answers)
Closed 6 years ago.
I have a tab separated text file f.txt like :
APPLE 10 5
BALL 20 6
CAT 30 7
I want the output to be
APPLE_10_5
BALL_20_6
CAT_30_7
I wrote the following to partially accomplish this, but I am stuck at the "paste" step. Can you help?
cat f.txt | cut -f 1,2,3 | paste ???
When the are sperated by one space using sed is a one liner.
sed -i "s/ /_/" input.txt

How to avoid the display of the 2 first line from a linux command output? [duplicate]

This question already has answers here:
What's the opposite of head? I want all but the first N lines of a file
(9 answers)
Closed 8 years ago.
I have a program that displays many line in the output
How I can make it display the all output except the first 2 lines?
easily using tail command:
tail -n+3
You could use awk
awk 'NR>2' file
In order to complete the triplet,
sed '1,2d' file

Bash to merge 2 consecutive lines in a file [duplicate]

This question already has answers here:
how can I combine these lines
(4 answers)
Closed 8 years ago.
I want convert this text on a given file:
87665
S
3243423
S
334243
N
...
to something like this:
87665,S
3243423,S
334243,N
...
I've been reading some similar questions, but it didn't work... is there a way to do this with a single line command in linux?
Thanks!
Using sed:
sed '$!N;s/\n/,/' filename
Using paste:
paste -d, - - < filename
paste would leave a trailing , in case the input has an odd number of lines.
Something like this might work for you:
$ awk 'NR%2{a=$0;next}{print a","$0}' file
87665,S
3243423,S
334243,N
To handle files with odd lines, you can do:
awk '{printf "%s%s", $0, NR%2?",":ORS}' file
Just for fun, a pure bash solution:
while IFS= read -r l1; do
read -r l2
printf '%s\n' "$l1${l2:+,$l2}"
done < file
If there's an odd number of lines, the last line will not have a trailing comma.

Resources