Awk, sed - concatenate lines starting with string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk, sed - concatenate lines starting with string
# 1  
Old 01-22-2013
Awk, sed - concatenate lines starting with string

I have a file that looks like this:
Code:
John Smith
http://www.profile1.com
http://www.profile2.com
http://www.profile3.com
Marc Olsen
http://www.profile4.com
http://www.profile5.com
http://www.profile6.com
http://www.profile7.com
Lynne Doe
http://www.profile8.com
http://www.profile9.com

I need to add every line that starts with "http://" to last line that doesn't start with "http://", so that the file becomes:
Code:
John Smith;http://www.profile1.com;http://www.profile2.com;http://www.profile3.com
Marc Olsen;http://www.profile4.com;http://www.profile5.com;http://www.profile6.com;http://www.profile7.com
Lynne Doe;http://www.profile8.com;http://www.profile9.com


Last edited by locoroco; 01-22-2013 at 08:16 PM..
# 2  
Old 01-22-2013
The following seems to do what you want:
Code:
awk '! /^http/ {
        if(nnl) printf("\n")
        nnl = 1
        printf("%s", $0)
        next
}
{       printf(";%s", $0)
}
END {   if(nnl) printf("\n")
}' input

Note that this will work with both http: and https: sites, but won't do the right thing if someone's name starts with http.
# 3  
Old 01-22-2013
Code:
awk 'NR==1&&!/^http/{n=$0}NR!=1&&!/^http/{print n,p; n=$0; p="";}/^http/{p=p";"$0;}END{print n,p;}' file

# 4  
Old 01-22-2013
This works with both http- and https-URLS and won't be confused by names starting with "http". @Don: good idea with the https, making the "s" optional would work in awk too.

Code:
sed '/^https*:/ !{ H ; s/.*// ; x ; p }
     /^https*:/ { s/^/;/ ; H }' /path/to/infile

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Delete all lines except a line starting with string

Shell : bash OS : RHEL 6.8 I have a file like below. $ cat pattern.txt hello txt1 txt2 txt3 some other text txt4 I want to remove all lines in this file except the ones starting with txt . How can I do this ? (4 Replies)
Discussion started by: omega3
4 Replies

3. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

4. Shell Programming and Scripting

How to delete lines starting with specific string?

Dear all, I would like to delete even lines starting with "N" together with their respective titles which are actually odd lines. Below is the example of input file. I would like to remove line 8 and 12 together with its title line, i.e., line 7 and 11, respectively.... (2 Replies)
Discussion started by: huiyee1
2 Replies

5. Shell Programming and Scripting

Concatenate lines with unique string AND number

In Bash using AWK or sed I need to convert the following file: ... numitem_tab0 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p> subitem_tab0 =<p></p><p></p> ... numitem_tab6 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>... (2 Replies)
Discussion started by: pioavi
2 Replies

6. Shell Programming and Scripting

Deleting lines not starting with numbers with sed

Title says all :p Thanks for your help (4 Replies)
Discussion started by: drbiloukos
4 Replies

7. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies

8. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies

9. Shell Programming and Scripting

sed / awk to concatenate lines until blank line

Sample input (line feed indicated by ) --------------- The red fox jumped over the brown fence of the red hous He then went into the orchard --------------- Desired Output --------------- The red fox jumped over the brown fence of the red house He then went into the orchard (11 Replies)
Discussion started by: dunstonrocks
11 Replies

10. Shell Programming and Scripting

Concatenate String through Awk

I want to concatenate any particular field of the file with any String say SSB....but i am not able to do it... I hv tried the following code....but its saying there is error in parsing it.. awk 'BEGIN { FS = "," ; OFS = "," ; } { for ( i = 1 ; i < 5 ; i++ ) {a=i;b="SSB"; print $1,$a$b,$3 } }'... (3 Replies)
Discussion started by: monu_munish
3 Replies
Login or Register to Ask a Question