Help with awk adding spaces.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk adding spaces.
# 1  
Old 02-23-2009
Help with awk adding spaces.

I have a file that contains...

Code:
    
  elm,mail
  elm,lisp,composer,cd,ls,cd,ls,cd,ls,zcat,|,tar,-xvf,ls,cd,ls,cd,ls,vi,ls,cd,ls,vi,elm,-f,ls,rm,ls,cd,ls,vi,vi,ls,vi,ls,cd,ls,elm,cd,ls,cd,ls,vi,vi,vi,ls,vi,ls,i,vi,ls,cp,cd,fg,ls,rm,cd,ls,-l,exit
  elm,mail,biff,elm,biff,elm,elm
  elm,ls
  elm,grep,mail,man,man,-k,fg,look,fg,elm,finger,elm,finger,cd,ls,cd,cd,ls,cd,ls,cd,ls,-l,date,more,ls,-l,more,ls,more,ls,rm,ls,cd,ls,mail,elm,look,fg,fg,cd,cd,ls,cd,ls,cd,ls,cd,ls,vi,man,ls,cd,cd,ls,vi,echo,man,-k,man,ls,cd,ls,biff,ls,elm,fg,exit
  elm,biff,biff,mail,elm,date,rlogin,-l,fg,elm,ls,biff,elm,mesg,finger,finger,talk,logout
  elm,ls,finger,finger,|,more,biff,matlab,clear,elm,rn,ls,matlab,mail,fg,elm,ls,exit
  elm,fk
  matlab
  elm,finger,finger,elm
  cd,ls,cd,ls,ls,-l,sccs,vi,vi,vi,vi,sccs,clear,fg,vi,sccs,ls,ls,-l,vi,vi,vi,vi,clear,sccs,vi,sccs,exit
  elm,mail,exit
  elm,fk,date,mail,exit

I want to print out only the lines that have three words. I use....

Code:
cat workeditf.txt | awk -F"," '{if (NF = 3) print }' > 1.txt

The answer I get...

Code:
  elm mail 
  elm lisp composer
  elm mail biff
  elm ls 
  elm grep mail
  elm biff biff
  elm ls finger
  elm fk 
  matlab  
  elm finger finger
  cd ls cd
  elm mail exit
  elm fk date

The answer I should get...

Code:
  
  elm mail exit

I delete all spaces after each line before I use the awk script. But after the awk script it has blank spaces added on the end of the wrong answers, which I think is causing me to get the wrong answer.

Any suggestions or what other code can I use?
# 2  
Old 02-23-2009
slight modification to your command

> awk -F"," 'NF==3' workeditf.txt > 1.txt
# 3  
Old 02-24-2009
Code:
grep '^[^,]*,[^,]*,[^,]*$' a.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sendmail with cat adding extra spaces in email body

when I try to read a file and send email using cat and sendmail: The email received having additional spaces.(Between the letters of words in the text) My code: export MAILTO="sa@y.com" export SUBJECT="mydomain PREPROD MONITOR AT ${DATE}" export... (5 Replies)
Discussion started by: visitsany
5 Replies

2. Shell Programming and Scripting

awk for removing spaces

HI, I have a file with lot of blank lines, how can I remove those using awk?? Thanks, Shruthi (4 Replies)
Discussion started by: shruthidwh
4 Replies

3. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

4. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

5. Shell Programming and Scripting

Adding character to spaces in a table

Hi All, I am trying to get the spaces in the below table to be fill up with a character " - ". For eg, coordinates 0202 is a space but i would want to fill up with " - ". Can anybody help ? Input: 04 D H ... (15 Replies)
Discussion started by: Raynon
15 Replies

6. Shell Programming and Scripting

adding spaces for a variable value

Hi, i have to form the header and add fillers(spaces) to it. I have done something like this. i have added 10 spaces at the end HDR="AAAABBBBCCNN " echo $HDR >> file1.dat but the spaces are not being stored in the file. How to add the spaces. (2 Replies)
Discussion started by: dnat
2 Replies

7. Shell Programming and Scripting

Adding spaces to record

Hi, I want to print spaces in a trailer record which is a single command. namely the unix command which i already have recs=`wc -l $TargetFileDir/myfile.txt|cut -c1-9`;export recs;echo 'PCPC.DXDINPT.FC0.INPUTFLE.PASS'`date +%Y%m%d``printf '%015d\n' $recs` >> $TargetFileDir/myfile1.txt I... (3 Replies)
Discussion started by: nvenkat010
3 Replies

8. Shell Programming and Scripting

adding spaces to a line

Is there any command to add spaces to a lline....say i need 50 spaces between the data like "aaabbbccc dddeeefff" or may be like this "aaaabbbbbbcccccdddddeeeffff " your help is appreciated. (4 Replies)
Discussion started by: mgirinath
4 Replies

9. Programming

Removing empty spaces and adding commas

I have a file which contains numbers as follows: 1234 9876 6789 5677 3452 9087 4562 1367 2678 7891 I need to remove the empty spaces and add commas between the numbers like: 1234,9876,6789,5677,3452, 9087,4562,1367,2678,7891 Can anyone tell me the command to do... (4 Replies)
Discussion started by: jazz
4 Replies

10. UNIX for Dummies Questions & Answers

Adding Trailing Spaces to a file

I have a text file which is not fixed width. I want to put trailing spaces to each line and make it a 100 byte fixed width file. Can someone please help me as soon as possible? Thanks, Denis (1 Reply)
Discussion started by: 222001459
1 Replies
Login or Register to Ask a Question