Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-12-2012, 03:00 PM
|
#1
|
LQ Newbie
Registered: Jan 2012
Posts: 7
Rep:
|
Extracting second line from multiple txt files and append to a file
Hi,
I am trying to use an awk command to extract the second line from a bunch of txt files (tab delimited) and append them to a new file.
I use the command below to extract a second line from a single txt file, and it works fine.
awk 'NR==2' old_file1.txt > new_file1.txt
I know I have to have some kind of a loop to go over all the input files but I am not very much familiar with awk commands, any help will be much appreciated.
Thanks!
|
|
|
08-12-2012, 03:21 PM
|
#2
|
LQ Addict
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,255
Rep:
|
|
|
|
08-12-2012, 03:30 PM
|
#3
|
Member
Registered: Apr 2005
Distribution: Debian
Posts: 82
Rep:
|
Code:
for FILE in *.txt; do sed -n '2p' $FILE; done > output
Just change *.txt to whatever. Could be a list of files separated by spaces too.
|
|
|
08-12-2012, 03:57 PM
|
#4
|
LQ Newbie
Registered: Jan 2012
Posts: 7
Original Poster
Rep:
|
Quote:
Originally Posted by i_joh
Code:
for FILE in *.txt; do sed -n '2p' $FILE; done > output
Just change *.txt to whatever. Could be a list of files separated by spaces too.
|
Thanks for the response. I tried that and it worked perfectly except for one thing, the order is not maintained. May be I should ask this: how are the input files processed? Isnt it by file name? I wanted my output to be the same order as the inputfiles so that I can know which output line is coming from which file.
|
|
|
08-12-2012, 04:10 PM
|
#5
|
Member
Registered: Apr 2005
Distribution: Debian
Posts: 82
Rep:
|
I get alphabetical sorting. What happens is that *.txt is replaced with all the .txt files in the current directory, each separated by a space. Then the first file name is put in $FILE and the command run. Then the second file name is put in $FILE and the command run, etc until all files are processed. An alternative would be to list the files manually in the order you want them listed, or sort them with the ls command (see man ls for options):
Code:
for FILE in $(ls -r); do sed -n '2p' "$FILE"; done
That would sort them in reverse.
|
|
|
08-12-2012, 04:13 PM
|
#6
|
LQ Newbie
Registered: Jan 2012
Posts: 7
Original Poster
Rep:
|
Quote:
Originally Posted by i_joh
Code:
for FILE in *.txt; do sed -n '2p' $FILE; done > output
Just change *.txt to whatever. Could be a list of files separated by spaces too.
|
Or is there a way to include a column in the output file containing the name of the input files? So every time a second line is read from the input file, it is appended to the output file with an extra column added containing the file name. This would be equally usefull if it is impossible to guarantee that the input files will be processed in the same order. Thanks!
|
|
|
08-12-2012, 04:16 PM
|
#7
|
Member
Registered: Apr 2005
Distribution: Debian
Posts: 82
Rep:
|
Quote:
Originally Posted by sal_x_sal
Or is there a way to include a column in the output file containing the name of the input files? So every time a second line is read from the input file, it is appended to the output file with an extra column added containing the file name. This would be equally usefull if it is impossible to guarantee that the input files will be processed in the same order. Thanks!
|
Code:
for FILE in *.txt; do echo "$FILE: $(sed -n '2p' $FILE)"; done > output
|
|
|
08-12-2012, 04:24 PM
|
#8
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,808
|
Code:
awk 'FNR==2{print FILENAME, $0}' *.txt > output
|
|
|
08-12-2012, 04:40 PM
|
#9
|
LQ Newbie
Registered: Jan 2012
Posts: 7
Original Poster
Rep:
|
Thank you both for the responses. Both work perfect and produced the same result. One little thing if it is easier to do in awk, otherwise I will have to do the hard way. Basically, the filenames are now mixed together with the first column of the input file. Is there a way to have a tab after the file name, so the input file name is in a different column? Thank you very much once again!
|
|
|
08-12-2012, 04:45 PM
|
#10
|
Member
Registered: Apr 2005
Distribution: Debian
Posts: 82
Rep:
|
Quote:
Originally Posted by sal_x_sal
Thank you both for the responses. Both work perfect and produced the same result. One little thing if it is easier to do in awk, otherwise I will have to do the hard way. Basically, the filenames are now mixed together with the first column of the input file. Is there a way to have a tab after the file name, so the input file name is in a different column? Thank you very much once again!
|
Code:
for FILE in *.txt; do echo -e "$FILE\t$(sed -n '2p' $FILE)"; done > output
I'd recommend you learn shell programming. It's kind of what makes Linux/BSD so much better than Windows.
|
|
|
08-12-2012, 04:48 PM
|
#11
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,808
|
Code:
awk -vOFS='\t' 'FNR==2{print FILENAME, $0}' *.txt > output
|
|
|
08-12-2012, 05:00 PM
|
#12
|
LQ Newbie
Registered: Jan 2012
Posts: 7
Original Poster
Rep:
|
Both commands work perfect. Thank you very much i_joh and ntubski for your time in helping me with this. I appreciate it.
@i_joh - I am so much sucked working with windows in my daily life. Once in a while when I have to use linux I realize how powerful it is. I try to learn linux on my own but since I am not using it much I end up forgetting a lot of stuff. Thanks once again and have a good day!
|
|
|
All times are GMT -5. The time now is 01:30 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|