Text manipulation with gawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text manipulation with gawk
# 1  
Old 12-15-2010
Text manipulation with gawk

Hi all,

I have a very long document in the following format:
Code:
[aa1 bb1] jkjlk
cc1
dd1

[aa2 bb2]
cc2
dd2

[aa3 aaa3 bb3]
cc3
dd3

and so on ...

The expected result should be a table
Code:
aa1 bb1;cc1;dd1
aa2 bb2;cc2;dd2
aa3 aaa3;cc3;dd3
...

Any idea, how to implement this?

Thanks a lot!

Best
Robert

Last edited by radoulov; 12-15-2010 at 04:40 AM.. Reason: Code tags, please!
# 2  
Old 12-15-2010
Is this a typo?

Code:
aa3 aaa3;cc3;dd3

I would expect:

Code:
aa3 aaa3 bb3;cc3;dd3

This User Gave Thanks to radoulov For This Post:
# 3  
Old 12-15-2010
Quote:
Originally Posted by radoulov
Is this a typo?

Code:
aa3 aaa3;cc3;dd3

I would expect:

Code:
aa3 aaa3 bb3;cc3;dd3

Indeed. Was a typo!

Thanks for correcting it.

Best
Robert
# 4  
Old 12-15-2010
Code:
awk '{ 
  sub(/^\[/, x, $1); sub(/\].*/, x, $1)
  }
  $1 = $1
  ' RS= FS='\n' OFS=\; infile

This User Gave Thanks to radoulov For This Post:
# 5  
Old 12-15-2010
Quote:
Originally Posted by radoulov
Code:
awk '{ 
  sub(/^\[/, x, $1); sub(/\].*/, x, $1)
  }
  $1 = $1
  ' RS= FS='\n' OFS=\; infile

Wow!!!! I am depply impressed.

I forgot to mention one little thing, which is not dramatic. But between the groups there is some rubbish text.

For instance:

Code:
[aa1 bb1] jkjlk
cc1
dd1

dssadas
dsadsadsa
sdadsadsa


[aa2 bb2]
cc2
dd2

sasadsa
sdasadds

[aa3 aaa3 bb3]
cc3
dd3

sadsad
sadsdsa

Any idea, how I can get rid of that?

Rest works fine!!!

Thanks a million!!!

Robert

Last edited by radoulov; 12-15-2010 at 05:01 AM.. Reason: Added code tags.
# 6  
Old 12-15-2010
or
Code:
awk '{print $2,$4,$5}' FS='[]\n[]' RS= OFS=\; infile

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 12-15-2010
Code:
awk '/\[/ { 
  sub(/^\[/, x, $1); sub(/\].*/, x, $1)
  $1 = $1; print
  }' RS= FS='\n' OFS=\; infile

---------- Post updated at 10:00 AM ---------- Previous update was at 09:55 AM ----------

Quote:
Originally Posted by Scrutinizer
or
Code:
awk '{print $2,$4,$5}' FS='[]\n[]' RS= OFS=\; infile

A variable number of entries like cc1, dd1 could be a problem.
This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Text Manipulation Help

Hello again unix.com, I have a text file in this format (line-by-line): hostname id password hostname id password hostname id password hostname id password I want it to become: hostname id password hostname id password hostname id password hostname id password (4 Replies)
Discussion started by: galford
4 Replies

2. Shell Programming and Scripting

Gawk - Text replace - need help

Hello, i am a working student and have to build a patch setup. I have to replace 2 lines in a .txt file. Can someone help me please. Now: wrapper.java.mainclass=org.tanukisoftware.wrapper. WrapperSimpleApp wrapper.java.classpath.2=../../jdk/lib/tools.jar replace with:... (12 Replies)
Discussion started by: Baefisch
12 Replies

3. Shell Programming and Scripting

Text manipulation help

Hello Unix.com, How can i generate links like this: i got http://upload.com/1/1.txt and i need to generate links from http://upload.com/1/1.txt to http://upload.com/1000/1000.txt Thanks in advance, Galford D. Weller (4 Replies)
Discussion started by: galford
4 Replies

4. Shell Programming and Scripting

gawk script to search and replace text in a flat file

Hi I am new to unix and newbie to this forum. I need help in writing a gawk script that search and replace particular text in a flat file. Input file text : ZIDE_CONTROL000 100000000003869920900000300000001ISYNC 000002225489 0000000002232122 20120321 16:40:53 ZIDE_RECORD000... (5 Replies)
Discussion started by: gkausmel
5 Replies

5. UNIX for Dummies Questions & Answers

text manipulation help

Hello again unix.com How can I extract from a large file in format: steve@aol.com steve hawkins Location of this member is bla bla bla sun@hotmail.com Sun Ying This member is using browser bla bla bla to another text in format: steve@aol.com steve hawkins sun@hotmail.com sun ying ... (5 Replies)
Discussion started by: galford
5 Replies

6. Shell Programming and Scripting

Text Manipulation Help

Hello unix.com people! How can I modify a text in format: A:B:C A:B:C A:B:C into C/A/B C/A/B C/A/B Note: Text is line by line and "C", "B", "A" fields are different each row. Thanks in advance. (7 Replies)
Discussion started by: galford
7 Replies

7. Shell Programming and Scripting

[HELP] Text manipulation... [HELP]

I need to know how can I remove all word after comma on each line. Like: jjkj,iiuiui,ijlkjkij,ookoo kijljlj,jhhkj,ijijkijkj,oijkijj kjkljlkj,kjkjlkjlkj,opok,okop to jjkj, kijljlj, ... (5 Replies)
Discussion started by: slutb3
5 Replies

8. UNIX for Dummies Questions & Answers

JOINING MULTIPLE LINES IN A TEXT FILE USING GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. I WANT THE DATA... (0 Replies)
Discussion started by: KANNI786
0 Replies

9. UNIX for Dummies Questions & Answers

Joining lines of a text file using GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. ... (0 Replies)
Discussion started by: KANNI786
0 Replies

10. Shell Programming and Scripting

Text Manipulation.

Hi I have only ever used awk and sed for basic requirements up until now. I have had to break a log down for multiple purposes. Using awk, sed and a date script. I am left with this: (message id, time of msg attempt, message id, domain name, time of msg completion) ... (4 Replies)
Discussion started by: Icepick
4 Replies
Login or Register to Ask a Question