How to preserve spaces in input fields with awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to preserve spaces in input fields with awk?
# 1  
Old 11-08-2013
Blade How to preserve spaces in input fields with awk?

I'm trying to do something pretty simple but its appears more complicated than expected... I've lines in a text file, separated by the comma and that I want to output to another file, without the first field.

Input file:
Code:
file1,item,      12345678
file2,item,      12345678
file2,item,      12345678

In output I've got the following:
Code:
,item, 12345678
,item, 12345678
,item, 12345678

As you can see the spaces before 12345678 are kind of merged into one space only.

My code:
Code:
cat myfile | while read l_line
   do
      v_OutputFile=$(echo $l_line | awk -F',' '{print $1}')
      echo $(echo $l_line | awk -F',' '{OFS = ",";$1=""; print $0}') >> ${v_OutputFile}
   done

The issue seems to come from the echo command that removes the spaces. If I do an echo with the quotes it works well. For instance:
Code:
echo 'file1,item,          12345678' | awk -F, -v OFS="," '{$1="";print $0}'

But I could not find a solution to work this out with the while loop. Even by putting the quotes in my input file, it stills removes the spaces.

Thanks for your help !
# 2  
Old 11-08-2013
Look here and ff. ( entire thread )
# 3  
Old 11-08-2013
Try the awk script below...
Code:
awk -F"[ ]" '{s="";n=split($1,a,",");for(i=2;i<=n;i++) s = s ? s","a[i] : ","a[i];$1=s;print}' file

# 4  
Old 11-08-2013
Putting the quotes at the right places will make it work:
Code:
cat file | while read l_line; do echo "$(echo "$l_line" | awk -F',' '{OFS = ",";$1=""; print $0}')"; done
,item,      12345678
,item,      12345678
,item,      12345678

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-08-2013
Ok to use the printf but I still have to use the echo command so I assume it will still remove the spaces no ?

---------- Post updated at 01:16 PM ---------- Previous update was at 12:44 PM ----------

Quote:
Originally Posted by RudiC
Putting the quotes at the right places will make it work:
Code:
cat file | while read l_line; do echo "$(echo "$l_line" | awk -F',' '{OFS = ",";$1=""; print $0}')"; done
,item,      12345678
,item,      12345678
,item,      12345678

mmmhh yeah it does the trick. Don't know what I did wrong at the first place because I tried to do the same but I probably put the quotes badly.
Thanks for this simple workaround !

---------- Post updated at 01:17 PM ---------- Previous update was at 01:16 PM ----------

Quote:
Originally Posted by shamrock
Try the awk script below...
Code:
awk -F"[ ]" '{s="";n=split($1,a,",");for(i=2;i<=n;i++) s = s ? s","a[i] : ","a[i];$1=s;print}' file

Thanks Shamrock it's a nice piece of code. I assume it's more powerfull to use such a syntax so maybe I'll integrate it into my script.
# 6  
Old 11-08-2013
Actually code posted by RudiC is far better than what I posted with the exception of the cat...so here's my version of the awk posted by RudiC...
Code:
awk -F"," '{$1="";OFS=",";print}' file

# 7  
Old 11-08-2013
In sed:
Code:
echo "file1,item,      12345678" | sed -e 's/^\([^,]*\)//'
,item,      12345678

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

3. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

Preserve spaces while reading character character

Hi All, I am trying to read a file character by character, #!/bin/bash while read -n1 char; do echo -e "$char\c" done < /home/shak/testprogram/words Newyork is a very good city. Newyorkisaverygoodcityforliving I need to preserve the spaces as thats an... (3 Replies)
Discussion started by: Kingcobra
3 Replies

5. Shell Programming and Scripting

awk - How to preserve whitespace?

Given a file: # configuration file for newsyslog # $FreeBSD: /repoman/r/ncvs/src/etc/newsyslog.conf,v 1.50 2005/03/02 00:40:55 brooks Exp $ # # Entries which do not specify the '/pid_file' field will cause the # syslogd process to be signalled when that log file is rotated. This # action... (10 Replies)
Discussion started by: jnojr
10 Replies

6. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

7. Shell Programming and Scripting

awk ignores fields with only spaces or empty

Hi, Does any one know how to avoid the scenario where awk ignores the fields having only spaces or empty fields? for instance, Data: "a","b","c","d",""," " code: awk -F, '{ print NF }' File the output I get is 4 instead of 6 do you know how to avoid this? (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

8. Shell Programming and Scripting

Preserve byte size of fields while pasting it to other file in unix

Preserve byte size of fields while pasting it to other file Hello I want to set two fields in a text file to be of size 20. how to do it using unix ? eg: ABC.txt Name | City I want Name and City both to be of size 20. Also If I am pasting it in other file the byte size should be... (1 Reply)
Discussion started by: dashing201
1 Replies

9. UNIX for Dummies Questions & Answers

Preserve byte size of fields while pasting it to other file

Hello I want to set two fields in a text file to be of size 20. how to do it using unix ? eg: ABC.txt Name | City I want Name and City both to be of size 20. Also If I am pasting it in other file the byte size should be preserved.i.e. If I want to append content of ABC.txt to other... (0 Replies)
Discussion started by: dashing201
0 Replies

10. UNIX for Dummies Questions & Answers

Find fields with no spaces in value

This is what I need to do I have a file that has a field with values like this 1111 2222 3333 4444 55555 666 333333333 444444444 I need for my command to out put only those fields that do not have spaces in them. So my output for the above file would be 333333333 444444444 how... (10 Replies)
Discussion started by: alfredo123
10 Replies
Login or Register to Ask a Question