Append lines for a specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append lines for a specific pattern
# 1  
Old 02-23-2012
Append lines for a specific pattern

Input:
Code:
09:43:46,538 INFO first text
10:45:46,538 INFO second
 text
11:00:46,538 INFO third
 more
 text

Output:
Code:
09:43:46,538 INFO first text
10:45:46,538 INFO second text
11:00:46,538 INFO third more text

The rule is to append all lines so each line contains this format
Code:
\d{2}:\d{2}:\d{2},\d{3}

Thx in advance.
# 2  
Old 02-23-2012
Try:
Code:
awk 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= infile

# 3  
Old 02-23-2012
A lazy way ...
Code:
sed '2,$s/^[0-9]/#&/g' input | xargs | tr '#' '\n'

---------- Post updated at 11:42 AM ---------- Previous update was at 11:35 AM ----------

@Scruti

I tried your code on a linux machine : the result is still printed in only one line

Code:
$ cat input
09:43:46,538 INFO first text
10:45:46,538 INFO second
 text
11:00:46,538 INFO third
 more
 text
$ awk 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= input
09:43:46,538 INFO first text10:45:46,538 INFO second text11:00:46,538 INFO third more text

# 4  
Old 02-23-2012
Code:
awk '/INFO/{if(NR!=1)print "";}{printf $0;}END{print "";}' input

Or

Code:
awk '/^[0-9]/{if(NR!=1)print "";}{printf $0;}END{print "";}' input

Guru.
# 5  
Old 02-23-2012
@ctsgnb: good point. Try:
Code:
awk --posix 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= infile

or use:
Code:
awk 'END{print RS} /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/&& NR>1{print RS}1' ORS= infile

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-23-2012
@Scruti :

The second one works fine (on a Ubuntu 11.04) :

Code:
$ awk --posix 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= input
awk: not an option: --posix
$ awk 'END{print RS} /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/&& NR>1{print RS}1' ORS= input
09:43:46,538 INFO first text
10:45:46,538 INFO second text
11:00:46,538 INFO third more text

---------- Post updated at 01:36 PM ---------- Previous update was at 01:20 PM ----------

Code:
awk '/INFO/&&NR>1{print z}{printf $0}END{print z}' input

# 7  
Old 02-23-2012
@ctsgnb, it is best to disable the format field in printf to avoid unintended interpretation of possible formatting characters in the input.
Code:
awk '/INFO/&&NR>1{print z}{printf "%s",$0}END{print z}' input

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match all lines in file where specific text pattern is less than

In the below file I am trying to grep or similar, all lines where only AF= is less than 0.4.. Thank you :). grep grep "AF=" ,+ .4 file file 12 112036782 . T C 34.0248 PASS ... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Delete lines that contain a pattern from specific line to the end.

Gents, I am trying to delete all lines which start with "H" character, but keeping the fist header. Example In the input file I will delete all lines starting from line 8 which contents character "H" to the end of the file. I try sed '8,10000{/^H/d;}' file But as don't know the end... (1 Reply)
Discussion started by: jiam912
1 Replies

3. Shell Programming and Scripting

Vi editor deleting lines with specific pattern

Hi, I need to delete all lines in the file using vi editor which start with word aternqaco. Please assist. aternqaco.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment aternqa.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

4. Shell Programming and Scripting

Want to get lines before specific pattern

Hi , I want to insert data into a new file after grepping specific pattern . for more info please read following for example: abc=12345678902222 def=45678904444 ------- ------- INAVLID ABC I want to "INAVLID ABC" grep above pattern from multiple files and want to write abc value and ... (3 Replies)
Discussion started by: vipin auja
3 Replies

5. Shell Programming and Scripting

How to concatenate lines with specific pattern?

How to concatenate lines with specific pattern? I have data dumped from a table into text file. In some occurrence the data row is split into two rows. Example: 12345678|Global Test|Global Test Task|My Request|Date|Date|Date|1|1| 12345679|Global Test2|Global Test Task2|My... (8 Replies)
Discussion started by: nixtime
8 Replies

6. Shell Programming and Scripting

Delete multiple lines starting with a specific pattern

Hi, just tried some script, awk, sed for the last 2 hours and now need help. Let's say I have a huge file of 800,000 lines like this : It's a tedious job to look through it, I'd like to remove those useless lines in it as there's a few thousands : Or to be even more precise : if line1 =... (6 Replies)
Discussion started by: Zurd
6 Replies

7. Shell Programming and Scripting

merging of 2 consecutive lines in a file for a specific pattern

Hi , I'm looking for a way to merge two lines only for a given pattern / condition. Input : abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre * fdsgfds/dsgf/sdfdsfasdd + -4.30 0.62 -49.5 45 sdfdsf cvbbv * sdfds/retret/asdsaddsa + ... (1 Reply)
Discussion started by: novice_man
1 Replies

8. 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

9. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

10. Shell Programming and Scripting

read from a specific pattern from one file and append it to another

Hi! Everyone, Say this file1 -------------- line 1 51610183 420001010 0010CTCTLEDPPOO 2151610183 line 2 2151610183 420001010 0030A2TH2 line 3 2151610183 420001010 0040A2TH3 line 4 2151610183 420001010 ... (0 Replies)
Discussion started by: kinkar_ghosh
0 Replies
Login or Register to Ask a Question