awk;sed appending line to previous line....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk;sed appending line to previous line....
# 1  
Old 06-23-2009
awk;sed appending line to previous line....

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 -
# 2  
Old 06-23-2009
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?
# 3  
Old 06-23-2009
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.
# 4  
Old 06-23-2009
Code:
/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

# 5  
Old 06-23-2009
Thanks Jim!

I am not quite sure I understand how the second line parses in english but the script works perfectly.
# 6  
Old 06-24-2009
Code:
cat yourfile | paste -d" " - -

# 7  
Old 06-24-2009
use this code:

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


Last edited by ahmad.diab; 06-24-2009 at 11:32 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

3. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

4. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

5. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

6. Shell Programming and Scripting

SED or AWK "append line to the previous line"

Hi, How can I remove the line beak in the following case if the line begin with the special char “;”? TEXT Text;text ;text Text;text;text I want to convert the text to: Text;text;text Text;text;text I have already tried to use... (31 Replies)
Discussion started by: research3
31 Replies

7. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

8. Shell Programming and Scripting

Sed - Appending a line with a variable on it

Hi, I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer... (4 Replies)
Discussion started by: eltinator
4 Replies

9. UNIX for Dummies Questions & Answers

SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day. I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file. I.e. ----Begin Original Datafile----- Condition = XXX Header Line 1 Header... (1 Reply)
Discussion started by: selkirk
1 Replies

10. Shell Programming and Scripting

Appending line/lines with sed

Hi folks, I need to append line or bulk of lines into a file. For example,I have the following section in opmn.xml file: <process-type id="OC4J_RTEadmin_NIR" module-id="OC4J"> <module-data> <category id="start-parameters"> <data... (28 Replies)
Discussion started by: nir_s
28 Replies
Login or Register to Ask a Question