I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one:
User1NameLast User1NameFirst User1Address
E-Mail:User1email
User2NameLast User2NameFirst User2Address
E-Mail:User2email
User3NameLast User3NameFirst User3Address
E-Mail:User3email
User4NameLast User4NameFirst User4Address
E-Mail:User4email
In English what I am looking for is each line that begins with the word "E-Mail" to be appended to the previous line so that all of the data for each user looks like this:
User1NameLast User1NameFirst User1Address E-Mail:User1email
User2NameLast User2NameFirst User2Address E-Mail:User2email
User3NameLast User3NameFirst User3Address E-Mail:User3email
User4NameLast User4NameFirst User4Address E-Mail:User4email
Thanks -
awk
June 23, 2009, 2:59pm
2
Well, without doing the entire thing for you
In awk
Use printf on the odd number lines. This allows you to put the output without a new-line at the end of the line.
Use print on the even lines - this will automatically print a new-line.
simple heh?
My example text doesn't contain all of the data, only the data that I am concerned about. Odd/even isn't a consistent variable to key off of. The only consistency that I have is that every line that begins with "E-Mail" should be appended to the previous line.
/home/jmcnama> cat t.awk
awk ' /^E-Mail/ {printf(" %s\n", $0); found=1; next}
!/^E-Mail/ {printf((found==1)? "%s" : "\n%s", $0); found=0 } END {print ""} ' filename
/home/jmcnama> cat filename
User1NameLast User1NameFirst User1Address
E-Mail:User1email
not email
just filler
User2NameLast User2NameFirst User2Address
E-Mail:User2email
User3NameLast User3NameFirst User3Address
E-Mail:User3email
more filler and junk
User4NameLast User4NameFirst User4Address
E-Mail:User4email
/home/jmcnama> t.awk
User1NameLast User1NameFirst User1Address E-Mail:User1email
not email
just filler
User2NameLast User2NameFirst User2Address E-Mail:User2email
User3NameLast User3NameFirst User3Address E-Mail:User3email
more filler and junk
User4NameLast User4NameFirst User4Address E-Mail:User4email
Thanks Jim!
I am not quite sure I understand how the second line parses in english but the script works perfectly.
cat yourfile | paste -d" " - -
use this code:
nawk '(NR%2) {printf"%s ",$0 ; next} ((NR+1)%2) {printf"%s\n",$0}' file.txt
input:-
User1NameLast User1NameFirst User1Address
E-Mail:User1email
User2NameLast User2NameFirst User2Address
E-Mail:User2email
User3NameLast User3NameFirst User3Address
E-Mail:User3email
User4NameLast User4NameFirst User4Address
E-Mail:User4email
output are:-
User1NameLast User1NameFirst User1Address E-Mail:User1email
User2NameLast User2NameFirst User2Address E-Mail:User2email
User3NameLast User3NameFirst User3Address E-Mail:User3email
User4NameLast User4NameFirst User4Address E-Mail:User4email
Here's one way to do it in perl, tested on Jim's file:
$
$ perl -ne 'BEGIN{$/=""}{s/\nE-Mail/ E-Mail/g; print}' file1
User1NameLast User1NameFirst User1Address E-Mail:User1email
not email
just filler
User2NameLast User2NameFirst User2Address E-Mail:User2email
User3NameLast User3NameFirst User3Address E-Mail:User3email
more filler and junk
User4NameLast User4NameFirst User4Address E-Mail:User4email
$
$
tyler_durden
What about this:
nawk '{printf "%s ",$0 ; getline ; print}' ahmad
take a sample data file provided by jim in the previous post and see what it does.
Does not work. I guess I didn't understand the data structure.
you can also check the following thread:
http://www.unix.com/shell-programming-scripting/109683-sed-awk-append-line-previous-line.html
or use this command
sed -n '/^;.*/!{$!{N;s/\(.*\)\n\n*\(;.*\)*/\1\2/g;P;D};p}' your-file.text