LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-30-2010, 11:21 PM   #1
stmas
LQ Newbie
 
Registered: Jan 2007
Posts: 24

Rep: Reputation: 0
Problem with the awk syntax?


have a very simple task using awk, but can't get the right syntax:

1. Have a file xxx.txt:

c1 c2 c3
AAA=c
c1 c2 c3
AAA=jjj


2. Need to add a string "TOTAL" to the end of each line containing 'AAA'

3. The following script does not work don't know why:

cat xxx.txt|awk '{if (index(LINE,'AAA') > 0) print $0 "TOTAL"; else print $0; next);'



4. This one prints only 'AAA' records:
cat xxx.txt|awk '/AAA/{print $0 "TOTAL"; next};'

Thanks!
 
Old 09-30-2010, 11:37 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Fancy sed?
Code:
sed '/^AAA/ s/$/ TOTAL/' file

Cheers,
Tink
 
Old 09-30-2010, 11:43 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk -F"=" '$1=="AAA"{ $0=$0" TOTAL" }1' file

Last edited by ghostdog74; 09-30-2010 at 11:44 PM.
 
1 members found this post helpful.
Old 09-30-2010, 11:45 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
#deleted#

Last edited by ghostdog74; 09-30-2010 at 11:52 PM.
 
Old 10-03-2010, 02:13 PM   #5
stmas
LQ Newbie
 
Registered: Jan 2007
Posts: 24

Original Poster
Rep: Reputation: 0
this one works too:

awk '/AAA/{print $0 "TOTAL";next}1'; file
 
Old 10-03-2010, 02:28 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Can you mark it as resolved if you're happy with the results, please?


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk AND syntax webhope Programming 14 05-25-2010 07:38 AM
while loop in awk function - syntax problem skuz_ball Programming 2 03-10-2010 02:59 AM
[SOLVED] AWK Syntax MTK358 Programming 9 03-02-2010 11:31 AM
awk syntax wakatana Programming 10 10-20-2009 01:51 PM


All times are GMT -5. The time now is 12:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration