AWK print last field including SPACES


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK print last field including SPACES
# 1  
Old 09-28-2012
Bug AWK print last field including SPACES

I have simple test.sh script, see below:
Code:
bill_code=`echo $record | awk -F"|" '{print $1}'`
Fullname=`echo $record | awk -F"|" '{print $3}'`
email=`echo $record | awk -F\ '{print $4}'`

The last field contains spaces: see csv below:
Code:
A0222|Y|DELACRUZ|-cc dell@yahoo.com-cc support@yahoo.com


When I run the program, then it return below:
Code:
A0222
DELACRUZ
-cc

Wherein the email was concatenated. I expect to have like below:
Code:
A0222
DELACRUZ
-cc dell@yahoo.com -cc support@yahoo.com

It appears that when this program hit the space then it treat as delimiter.


Please advise, what would be the best code for this?


Thanks in advance.
quay

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by vbe; 09-28-2012 at 08:54 AM..
# 2  
Old 09-28-2012
try..

Code:
 
echo "$record"|awk -F"|" '{print $1"\n"$2"\n"$NF}'

# 3  
Old 09-28-2012
oops

thanks vidyadhar85, but same issue.
# 4  
Old 09-30-2012
When I tried running the command:
Code:
email=`echo $record | awk -F\ '{print $4}'`

I got an awk: no program given diagnostic message. I don't know how you got -cc out of it.

If you change this awk to match the other two awk commands, i.e.:
Code:
email=`echo $record | awk -F"|" '{print $4}'`

it seems to work for me.
# 5  
Old 10-03-2012
Thanks for your sharing. I appreciate and learn from that method. Meantime, I manage to achieve what I am trying to get, by using sed command however the record in last field , I should put "_" underscore, because spaces always bring treat as delimiter. To share, see below:
With this record:
Code:
A0222|Y|DELACRUZ|-cc_dell@yahoo.com_-cc_support@yahoo.com

Code:
     bill_code=`echo $record | awk -F"|" '{print $1}'`
     AUTH=`echo $record | awk -F"|" '{print $3}'`
     email=`echo $record | awk -F"|" '{print $4}'`
     
     echo $bill_code
     echo $AUTH
     echo $email
     echo $email  | sed 's/_/ /g'

Result are:
Code:
A0222
DELACRUZ
-cc_dell@yahoo.com_-cc_support@yahoo.com
-cc dell@yahoo.com -cc support@yahoo.com      --> this is goal.

Space is still my riddle, how to figure it out. Above is only a workaround.
Thanks

Last edited by Scott; 10-03-2012 at 07:52 AM.. Reason: Please use code tags
# 6  
Old 10-03-2012
Code:
record='A0222|Y|DELACRUZ|-cc dell@yahoo.com-cc support@yahoo.com'
OLDIFS="$IFS"
IFS="|"
set -- $record
echo "$1"
echo "$2"
echo "$3"
echo "$4"
IFS="$OLDIFS"

producing
Code:
A0222
Y
DELACRUZ
-cc dell@yahoo.com-cc support@yahoo.com

# 7  
Old 10-03-2012
Code:
$ cat file
A0222|Y|DELACRUZ|-cc_dell@yahoo.com_-cc_support@yahoo.com

$ awk -F "|" '{ for(i=1;i<=NF;i++){if(i !=2){gsub("_"," ",$i);print $i}}}'
A0222
DELACRUZ
-cc dell@yahoo.com -cc support@yahoo.com

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print string with removing all spaces

Hi all, I want to set 10 set of strings into a variable where: removing all spaces within each string change the delimiter from "|" to "," Currently, I've the below script like this:Table=`ten character strings with spaces in-between and each string with delimiter "|" | tr -d ' ' |... (7 Replies)
Discussion started by: o1283c
7 Replies

2. Shell Programming and Scripting

awk - CSV file - field with single or multiple spaces

Hi, In a csv file, I want to select records where first column has zero or multiple spaces. Eg: abc.csv ,123,a ,22,b ,11,c a,11,d So output should be: ,123,a ,22,b ,11,c Please advise (5 Replies)
Discussion started by: vegasluxor
5 Replies

3. Shell Programming and Scripting

awk - print columns with text and spaces

Hi, I'm using awk to print columns from a tab delimited text file: awk '{print " "$2" "$3" $6"}' file The problem I have is column 6 contains text with spaces etc which means awk only prints the first word. How can I tell awk to print the whole column content as column 6? Thanks, (10 Replies)
Discussion started by: keenboy100
10 Replies

4. UNIX for Dummies Questions & Answers

Print files with spaces using awk

When I use: find . -ls | awk 'BEGIN { OFS = ";"} {print $1,$2,$11}'I get a nice result, yet the files with spaces in it show only the first word and all other characters after the blank space are not printed. e.g. 'file with a blank space' is printed file 'file_with a blank space' is... (7 Replies)
Discussion started by: dakke
7 Replies

5. Shell Programming and Scripting

Print filenames with spaces using awk

Hello all, I want to list the file contents of the directory and number them. I am using la and awk to do it now, #ls |awk '{print NR "." $1}' 1. alpha 2. beta 3. gamma The problem I have is that some files might also have some spaces in the filenames. #ls alpha beta gamma ... (7 Replies)
Discussion started by: grajp002
7 Replies

6. UNIX for Dummies Questions & Answers

[awk] print from field n to field x

Hi, I'm trying to print every line from first field to the fourth from a file containing more. $ cat input a b c d e f g a b c d e f gI'm trying awk '{for (i=1; i <= NF-3; i++) print $i}' awkTest.datbut it printsa b c d a b c dSo, I easily guess I'm wrong. :) Of course, I want:a b... (5 Replies)
Discussion started by: daPeach
5 Replies

7. Shell Programming and Scripting

How to print arguments along with spaces using awk

Hi All, file_1.txt contains aaa bbbb hhhh vvvvv mmmmm iiiii What i want is to search for the first coloumn of each line using awk.i.e as below: awk '/aaa/ {printf(<>)}' file_1.txt The print part (<>) should contain all the values(or coloumns ) in the particular line(here it... (8 Replies)
Discussion started by: jisha
8 Replies

8. UNIX for Dummies Questions & Answers

Reading a line including spaces

Hi All, I have a script that reads a file and echo it back to std out. Test.txt 1aaaaaaaaaaa . The script is ReadLine.sh #!/bin/ksh cat $1 | while read file do echo $file done I invoke the script as ReadLine.sh Test.txt The output that I get is (1 Reply)
Discussion started by: aksarben
1 Replies

9. Shell Programming and Scripting

including spaces in awk output

I need to tweek my awk output: #cat filename ab cd ef:ghi:jk lm:nop qrs #cat filename | awk '{ for(i=3;i<NF+1;i++) printf $i}' ef:ghi:jklm:nopqrs I would like the ouput to include the original spaces from columns 3 on: ef:ghi:jk lm:nop qrs any suggestions? (4 Replies)
Discussion started by: prkfriryce
4 Replies

10. Shell Programming and Scripting

Read files including spaces

I am accessing two files. I am using read command to read from the files. For the first file, I need read the fields delimited by spaces, and for the other file, I need to read the whole line as a single field including the spaces. When I used read command for the second file, the spaces... (4 Replies)
Discussion started by: kumariak
4 Replies
Login or Register to Ask a Question