LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-01-2010, 12:21 AM   #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 10-01-2010, 12:37 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

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

Cheers,
Tink
 
Old 10-01-2010, 12:43 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Last edited by ghostdog74; 10-01-2010 at 12:44 AM.
 
1 members found this post helpful.
Old 10-01-2010, 12:45 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Last edited by ghostdog74; 10-01-2010 at 12:52 AM.
 
Old 10-03-2010, 03: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, 03:28 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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 08:38 AM
while loop in awk function - syntax problem skuz_ball Programming 2 03-10-2010 03:59 AM
[SOLVED] AWK Syntax MTK358 Programming 9 03-02-2010 12:31 PM
awk syntax wakatana Programming 10 10-20-2009 02:51 PM


All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
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