Text manipulation with gawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text manipulation with gawk
# 8  
Old 12-15-2010
Quote:
Originally Posted by radoulov
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 ----------

I get an syntax errror at the $1 = $1?

Did not find the error by myself.

Thanks
Robert
# 9  
Old 12-15-2010
Quote:
Originally Posted by radoulov
A variable number of entries like cc1, dd1 could be a problem.
You're right if that would be the case Smilie
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 12-15-2010
Quote:
Originally Posted by Robert_M
---------- Post updated at 10:00 AM ---------- Previous update was at 09:55 AM ----------

I get an syntax errror at the $1 = $1?
Please post the exact command (you know, spaces and newlines matter) and the exact error message you're getting. Just copy/paste what you see in your terminal.

---------- Post updated at 10:09 AM ---------- Previous update was at 10:05 AM ----------

Quote:
Originally Posted by Scrutinizer
You're right if that would be the case Smilie
Well, if that's not the case, a simple loopless sed will be sufficient Smilie

Code:
sed -n '
  /\[/{s/\[\([^]]*\)\].*/\1/;N;N;s/\n/;/g;p;}
  ' infile

This User Gave Thanks to radoulov For This Post:
# 11  
Old 12-15-2010
BIG THANKS!!!

Hi radoulov and Scrutinizer,

checked now both solutions. And they both lead to the the same result.

When I checked radoulov's solutions, I used "awk '/\[/ {sub(/^\[/, x, $1); sub(/\].*/, x, $1) $1 = $1; print}' RS= FS='\n' OFS=\; test.txt", which resulted in an error. If I copied and pasted directly the code, it worked. Have no idea why...

However, thank you very much for your quick help. It saved me a lot of time!!!!

Best
Robert
# 12  
Old 12-15-2010
Quote:
Originally Posted by Robert_M
Hi radoulov and Scrutinizer,

checked now both solutions. And they both lead to the the same result.
Good Smilie

Quote:
When I checked radoulov's solutions, I used "awk '/\[/ {sub(/^\[/, x, $1); sub(/\].*/, x, $1) $1 = $1; print}' RS= FS='\n' OFS=\; test.txt", which resulted in an error. If I copied and pasted directly the code, it worked. Have no idea why...
Because, as I said, newlines matter. You need either a semicolon or a newline between sub(/\].*/, x, $1) and $1 = $1.

Quote:
However, thank you very much for your quick help. It saved me a lot of time!!!!
You're welcome!
# 13  
Old 12-15-2010
Quote:
Originally Posted by radoulov
Good Smilie

Because, as I said, newlines matter. You need either a semicolon or a newline between sub(/\].*/, x, $1) and $1 = $1.

You're welcome!
What do you mean with this? How do I add a newline?

Best
Robert
# 14  
Old 12-15-2010
I mean that this:

Code:
awk '/\[/ { 
  sub(/^\[/, x, $1); sub(/\].*/, x, $1)     <-- You need a newline here!
  $1 = $1; print
  }' RS= FS='\n' OFS=\; infile

and this:

Code:
awk '/\[/ { 
  sub(/^\[/, x, $1); sub(/\].*/, x, $1) $1 = $1; print
  }' RS= FS='\n' OFS=\; infile

... are different.
You need a newline after the second sub command.
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