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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to awk or grep the last column in file when date on column contains spaces?
# 1  
Old 02-12-2014
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

Code:
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 Implementations UK
APM00111803814  server_2        96138   GWP Production-UK
APM00111803814  server_2        96109   GWP Production - US

I need to be able to read this file and define a variable for each entry in each column. The last column has spaces but I want to be able to read it a one variable. I am trying to put it into a while loop but the last column will only read the first word/characters (for example in the first line Corp)

For example

Code:
while read c1 c2 c3 c4
do
echo "$c1 $c2 $c3 $c4"
done < file.txt

Obviously this does not work. Can anybody help me out with this?

Last edited by Scrutinizer; 02-12-2014 at 08:07 AM.. Reason: code tags also for data
# 2  
Old 02-12-2014
Why would it not work with a while read loop? Have you tried it?

Last edited by Scrutinizer; 02-12-2014 at 08:16 AM..
# 3  
Old 02-12-2014
My apologies. It actually does work. My bad. There are other columns after C4 which I did not paste and it picks these up as well. I just need to make sure that the column with the spaces is the last column and the while loop will work. Thanks for you reply.
# 4  
Old 02-12-2014
If you specify less parameters than there are fields then any leftover data is assigned to the last parameter.

EDIT: As you've noticed Smilie.

If there's more data afterwards (that you don't want) then you could pre-process the file with cut.
# 5  
Old 02-14-2014
This will give last complete coloumn of the file.

Code:
cut -c40- file1


Last edited by Franklin52; 02-14-2014 at 05:36 AM.. Reason: Please use code tags
# 6  
Old 02-19-2014
Try this:

Code:
nawk '{ f4="";
        for ( i=4; i<= NF; i++)
                f4=f4 $i;
        print $1, $2, $3, "<", f4, ">"; }'

# 7  
Old 02-19-2014
Can you post or attach the actual input file and specify exactly which columns you want in the output...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

2. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

3. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

4. Shell Programming and Scripting

Compare the second column of a file with the second column of another in awk

Hi, I know that this topic has been discussed in the past and I've tried to follow all the guidelines. Anyhow, I following describe my problem. I have a file (file1 , no. records = 67) containing pairs of IP addresses as follows (with single space as delimiter between the fields): example... (5 Replies)
Discussion started by: amarn
5 Replies

5. Shell Programming and Scripting

Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one: 192.168.1.1 CAXY0_123 10ABFL000001 # Comment 192.168.1.2 CAYZ0_123 10ABTX000002 # Comment 192.168.2.1 FLXY0_123 11ABCA000001 ... (4 Replies)
Discussion started by: TheNovice
4 Replies

6. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

7. 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

8. Shell Programming and Scripting

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: 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 ... (14 Replies)
Discussion started by: donisback
14 Replies

9. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question