|
Prev: NYC LOCAL: Wednesday 25 June 2008 NYLUG: Mark Tolliver on Application Security for Open Source Software
Next: Use firefox as if I were logged in another computer
From: Ela on 24 Jun 2008 05:10 Under the same directory, there are a lot of files names in this system: 3-character-prefix+anything How can I read the 2nd line of the contents and concatenate them automatically? e.g. M01ABC content: Dear Friends, Meeting will be held at 10 M01DEF content: Dear guys, Meeting will be held at 19 M02DEF content: Hi, Meeting will be held at 19 So to generate, say: M01 content: Meeting will be held at 10Meeting will be held at 19 M02 content: Meeting will be held at 19
From: Dave B on 24 Jun 2008 05:43 Ela wrote: > Under the same directory, there are a lot of files names in this system: > > 3-character-prefix+anything > > How can I read the 2nd line of the contents and concatenate them > automatically? > > > e.g. > > M01ABC content: > Dear Friends, > Meeting will be held at 10 > > M01DEF content: > Dear guys, > Meeting will be held at 19 > > M02DEF content: > Hi, > Meeting will be held at 19 > > > So to generate, say: > > M01 content: > Meeting will be held at 10Meeting will be held at 19 > > M02 content: > Meeting will be held at 19 awk 'substr(FILENAME,1,3)!=lastf { if(lastf) { print lastf " content:";print line } line="" lastf=substr(FILENAME,1,3) } FNR==2 {line=line $0; nextfile} END {print lastf " content:";print line}' file1 file2 ... -- D.
From: Hermann Peifer on 24 Jun 2008 06:11 On Jun 24, 11:10 am, "Ela" <e...(a)yantai.org> wrote: > Under the same directory, there are a lot of files names in this system: > > 3-character-prefix+anything > > How can I read the 2nd line of the contents and concatenate them > automatically? > > e.g. > > M01ABC content: > Dear Friends, > Meeting will be held at 10 > > M01DEF content: > Dear guys, > Meeting will be held at 19 > > M02DEF content: > Hi, > Meeting will be held at 19 > > So to generate, say: > > M01 content: > Meeting will be held at 10Meeting will be held at 19 > > M02 content: > Meeting will be held at 19 Here another option. Please note that nextfile is a gawk extension. (The script would work without and just be somewhat slower.) $ cat script.awk FNR == 1 { out = substr(FILENAME, 1, 3) a[out] next } FNR == 2 { printf "%s", $0 > out nextfile } END { for (i in a) { print "" > i close(i) } } $ gawk -f script.awk file1 file2 ...
From: Ela on 24 Jun 2008 06:35 "Dave B" <daveb(a)addr.invalid> wrote in message news:g3qfqb$4qk$1(a)registered.motzarella.org... > Ela wrote: > >> Under the same directory, there are a lot of files names in this system: >> >> 3-character-prefix+anything >> >> How can I read the 2nd line of the contents and concatenate them >> automatically? >> >> >> e.g. >> >> M01ABC content: >> Dear Friends, >> Meeting will be held at 10 >> >> M01DEF content: >> Dear guys, >> Meeting will be held at 19 >> >> M02DEF content: >> Hi, >> Meeting will be held at 19 >> >> >> So to generate, say: >> >> M01 content: >> Meeting will be held at 10Meeting will be held at 19 >> >> M02 content: >> Meeting will be held at 19 > > > awk 'substr(FILENAME,1,3)!=lastf { > if(lastf) { > print lastf " content:";print line > } > line="" > lastf=substr(FILENAME,1,3) > } > FNR==2 {line=line $0; nextfile} > END {print lastf " content:";print line}' file1 file2 ... > > -- > D. Do I need to supply all the file names one by one by myself, i.e. how to call this script? In our last correspondence, I have to do that by myself...
From: Dave B on 24 Jun 2008 06:34
Ela wrote: >> awk 'substr(FILENAME,1,3)!=lastf { >> if(lastf) { >> print lastf " content:";print line >> } >> line="" >> lastf=substr(FILENAME,1,3) >> } >> FNR==2 {line=line $0; nextfile} >> END {print lastf " content:";print line}' file1 file2 ... >> >> -- >> D. > > Do I need to supply all the file names one by one by myself, i.e. how to > call this script? In our last correspondence, I have to do that by myself... That depends. If your files are called "M01xxxx", "M01xxxx", "M02xxxx", "M03xxxx", you can probably just say M0*, M*, or even * if the directory contains only those files. The shell will expand them in an ordered list. -- D. |