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
# 8  
Old 07-21-2009
Please explain

Quote:
Originally Posted by Franklin52
Or plain with awk:

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

Regards

Hi Franklin,
Could you please explain your command, if you don't mind?


Regards,
Don[COLOR="#738fbf"]
# 9  
Old 07-21-2009
Quote:
Originally Posted by donisback
Hi Franklin,
Could you please explain your command, if you don't mind?


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

Explanation

Empty the fields 1, 2 and 3:

Code:
$1=$2=$3=""

Remove the spaces (field separators) of the empty fields:

Code:
sub("   ","")}

Means true to awk and the default action of awk is to print the record:

Code:
1

Regards
# 10  
Old 07-21-2009
cut can do this..

Code:
-bash-3.2$ cat test
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
07-15-09  06:07AM                10867 ABC_69 ER194561 test1.zip
07-15-09  06:07AM                10867 ABC_69 ER194561 test2.ZIP
07-15-09  06:07AM                10867 ABC_69 ER194561 test3.ZIP
-bash-3.2$ cat test | tr -s " " | cut -d ' ' -f4-
ABC_123_ER19057320090714082723.ZIP
ABC_123_ER19155920090714082842.ZIP
ABC_123_ER19257020090714083003.ZIP
ABC_123_ER19456120090714083105.ZIP
ABC_123 4507131004299717363.ZIP
ABC_69 ER194561.ZIP
ABC_69_ER194631.ZIP
ABC_69_ER195427.ZIP
ABC_69_ER195428.ZIP
ABC_69 ER194561 test1.zip
ABC_69 ER194561 test2.ZIP
ABC_69 ER194561 test3.ZIP
-bash-3.2$

# 11  
Old 10-15-2009
Hi Franklin and ryandegreat,
Thanks for the reply.Both your commands work fine as long as there is only one space in the fourth field.
If the fourth field has more than one space then, it is reducing the space in it. e.g.

My input is:
Code:
10-14-09  08:57AM               164117 091008 ABC DEF
10-14-09  08:57AM               304435 091009  ABC DEF
10-14-09  08:57AM               199438 091013   ABC DEF
10-14-09  08:57AM                  974 10_09_2009.160923090008095.abc.def

My output is:
Code:
091008 ABC DEF
091009 ABC DEF
091013 ABC DEF
10_09_2009.160923090008095.abc.def

# 12  
Old 10-15-2009
My command shouldn't behave that way ...

Last edited by radoulov; 10-15-2009 at 09:35 AM..
# 13  
Old 10-15-2009
Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors:

Code:
awk -F" |!" '{$1=$2=$3="";sub(" {1,}","")}1' file | sed 's/[^ ]* //'

# 14  
Old 10-15-2009
The command doesn't work ..
My output should not trim the spaces in the fourth field. It's not necessary to use awk command. Please tell me if there is any other way to extract the fourth field. The problem is that the fourth field is actually the file name in the ftp and I am automating it.
So, it has to copy the file.

---------- Post updated at 07:57 PM ---------- Previous update was at 07:51 PM ----------

Hi Radulov,
Your command works great!! Thanks a ton.

Would request you to post explanation please...
Don
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