Use awk to have the fourth column with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use awk to have the fourth column with spaces
# 1  
Old 07-16-2009
Use awk to have the fourth column with spaces

Hi Gurus,
We have a ftpserver from which we do a dir command and output it to a local file.
The content of the ftpfile is:
Code:
07-15-09  06:06AM                 5466 ABC_123_ER19057320090714082723.ZIP
07-15-09  06:07AM                 3801 ABC_123_ER19155920090714082842.ZIP
07-15-09  06:07AM                 2034 ABC_123_ER19257020090714083003.ZIP
07-15-09  06:07AM                 5346 ABC_123_ER19456120090714083105.ZIP
07-15-09  06:07AM                50188 ABC_123 4507131004299717363.ZIP
07-15-09  06:07AM                10867 ABC_69 ER194561.ZIP
07-15-09  06:07AM                73183 ABC_69_ER194631.ZIP
07-15-09  06:07AM                 1576 ABC_69_ER195427.ZIP
07-15-09  06:07AM                 5880 ABC_69_ER195428.ZIP

I generally use awk '{print $4}' ftpfile. But, I realized that the filename in the ftp might contain spaces.
So I came up with the command:
Code:
awk '{i=4;while (NF>=i) {print $i;i++}}' ftplist

But the above command prints new line for every print I give. I want it in the same line. I tried using cut but it doesn't help.
On an ad-hoc basis, I am currently using the below command:
awk '{if (NF!=4) {print $4,$NF}else {print $4}}' ftplist
The above command prints what I want but, I assume here that the filename will have only one space.
Please suggest.
# 2  
Old 07-16-2009
Try:
Code:
awk '{i=4;while (NF>=i) {printf("%s ", $i);i++};printf "\n"}' ftpfile

# 3  
Old 07-16-2009
Or:

Code:
awk 'sub(/[^ ]* *[^ ]* *[^ ]* */,"")' ftpfile

If you have GNU awk (gawk) you can avoid the repeating pattern using the --re-interval option.

And, of course, you can use sed for this.
# 4  
Old 07-16-2009
Or plain with awk:

Code:
awk '{$1=$2=$3="";sub("   ","")}1' file

Regards
# 5  
Old 07-16-2009
Thanks

Thanks that command helped. I have one more doubt. I am unable to use shell variables in awk. Could you help me again with that? I have a sample file like this:
Code:
PROJECT PATH
-----------------
ABC  /ABC/Datafiles/FILES
CLNT /CLNT/DataFiles/FILES 
BHL    /BHL/MISC/FILES
JMH /JMH/DataFiles/FILES
SMS /CLA/Datafiles/FILES
PCS /CLA/Datafiles/FILES

Now I have a script where I allow the user to enter the name of project based on which I grep the file and give the path. But here too, I realized that I am doing grep on the entire file rather than on only first column. The problem for that is the user can give anything on the given line and it might result in undesirable output.

My desired output is if the user gives PCS, he should see /CLA/DATAFILES/FILES

I initially used :
grep "$a" filename|awk '{print $2}'
where a is the name of the project which user gives.
I tried to use:
awk '{if ($1=$a) {print $2}else{print "WRONG INPUT"}}' filename

But the above awk doesn't work. Please help
Thanks
# 6  
Old 07-16-2009
You could do something like:

Code:
awk 'BEGIN {
  printf "Enter project name: "
  getline name < "-"
}
$1==name {print $2}' file

# 7  
Old 07-16-2009
Thanks

I found one more alternative:
awk '{if ( $1== "'"$a"'") {print $3}}' filename

Enclose the shell variable with (double quotes, single quotes,double quotes)

Your script also works..

Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

2. Shell Programming and Scripting

[Solved] Extract First character in fourth column

Hi Experts, I am new to UNIX. One of my file records are like below 220 IN C/A 515013 NULL NULL 220 IN C/A 515017 NULL NULL 225 IN C/A 333701 NULL NULL 225 IN C/A 515034 NULL NULL 225 IN C/A 499201 NULL NULL 225 IN C/A 499202 NULL NULL The above mentioned records delimiter is... (4 Replies)
Discussion started by: suresh_target
4 Replies

3. Shell Programming and Scripting

Remove the first character from the fourth column only if the column has four characters

I have a file as follows ATOM 5181 N AMET K 406 12.440 6.552 25.691 0.50 7.37 N ATOM 5182 CA AMET K 406 13.685 5.798 25.578 0.50 5.87 C ATOM 5183 C AMET K 406 14.045 5.179 26.909 0.50 5.07 C ATOM 5184 O MET K... (14 Replies)
Discussion started by: hasanabdulla
14 Replies

4. Shell Programming and Scripting

replace by match on fourth column

Hi friends, My input file is this way chr1 100 200 "abc" chr1 350 400 "abc" chr2 450 600 "def" chr2 612 780 "def" How do I make this file into chr1 100 400 "abc" chr2 450 780 "def" This is basically matching on the fourth column and taking the minimum of second column and the... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

5. Shell Programming and Scripting

Extracting 3rd column using awk from file with spaces.

BAQ001 /dev/rdsk/c2t0d7 1C13 (M) RW 69053 The line above is from a text file. I want to use awk to extract the value in the third column 1C13. I just can't seem to get the syntax right or something. Any help would be appreciated. Thanks, (5 Replies)
Discussion started by: ricnetman
5 Replies

6. Shell Programming and Scripting

delete empty spaces at specific column

Hi All, If anybody could help me with my scenario here. I have a statement file. Example of some content: DATE DESC Debit Credit Total Rate 02-Jan-08 Lodgement 200.00 1200.00 2.51 14-Sep-07 Withdrawal 50.00 1000.00 ... (8 Replies)
Discussion started by: yonez
8 Replies

7. Shell Programming and Scripting

How to manipulate first column and reverse the line order in third and fourth column?

How to manipulate first column and reverse the line order in third and fourth column as follws? For example i have a original file like this: file1 0.00000000E+000 -1.17555359E-001 0.00000000E+000 2.00000000E-002 -1.17555359E-001 0.00000000E+000 ... (1 Reply)
Discussion started by: Max Well
1 Replies

8. Shell Programming and Scripting

take last column includning spaces also

hi i have a file with content, i need to extract last column from each line including spaces and form a string i used echo "$LINE" | awk '{print $1,$2}' | grep '^\ 33333030 30303030 30303030 CTL1330000000000 44453932 33353237 36333030 0000DE9235276300 30305453 30353934 44453932... (4 Replies)
Discussion started by: Satyak
4 Replies

9. Shell Programming and Scripting

need help to remove spaces from first column

Hi, Here is sample data which I have: column#1 column#2 column#3 001A 50005 ROCKER ADJ 00010000100018UTIRR 001A 50020 CRANKSHAFT 0003445ES 001A 52201 SPARKPLUG ... (4 Replies)
Discussion started by: tayyabq8
4 Replies

10. UNIX for Dummies Questions & Answers

removing trailing spaces of a particular column in a file

Hi, I am currently confused. Suppose I have a file something like the one below. 4299|raj Telecommunications|12||||| 4302|anjali International Ltd.|86|ritchie||dong|(000)2890 9993 |(222)4881 3689 4305|フィデュシアリ・ト-スト・インター...ショ...ル投資顧問株式会社 |112||||01-9211-1931 |08-3677-1985 Now... (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question