awk get rid of space in end of field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk get rid of space in end of field
# 1  
Old 12-31-2009
awk get rid of space in end of field

Hello.

I'm using a file to "grep" in a 2nd one (with awk)
Code:
cat  file1
2 first user   
9 second user 
1 third user  (with a space after  user)

I want to get the line except the 1st field so I do :
Code:
field=$(gawk '{$1 =""; print $0}' file | sed 's/^ //')

It works but it deletes space or tabulations at the end of the field.

So when I use the variable field to search in an other file (/etc/passwd for example) it doesn't work
Code:
cat file2
g92i1234:x:1001:1001:first  user:/home/g92i1234:/bin/bash
g91i1234:x:1002:1002:second  user:/home/g91i1234:/bin/bash
r95i1234:x:1003:1003:third user  :/home/r95i1234:/bin/bash

gawk -F":"  -v TOFIND="$field"  '$5 == TOFIND  {print $0 } file2'

It works if I replace == by ~ but that's not exactly what I want

How can I preserve the missing space

Thank you and Happy new year for those who are already in 2010 and for those who will be soon Smilie

Last edited by Scott; 12-31-2009 at 06:14 PM.. Reason: Please use code tags
# 2  
Old 12-31-2009
Hi.

Normally using double quotes (") around your assignment, or usage would solve this.

Code:
$ cat file1
2 first user 
9 second user 
1 third user 

$ f=$(cut -d\  -f2- file1)

$ echo "\"$f\""           
"first user 
second user 
third user "



$ f="$(cut -d\  -f2- file1)"

$ echo \"$f\"
"first user second user third user "

Not sure if that's what you're asking, though.
# 3  
Old 12-31-2009
Actually, the space is lost when $0 is recalculated in response to the first field being assigned a value ($1 ="").

Happy New Year to all,
alister
# 4  
Old 12-31-2009
Thank you. It's working with cut. The space is not deleted. Smilie
# 5  
Old 12-31-2009
Quote:
Originally Posted by alister
Actually, the space is lost when $0 is recalculated in response to the first field being assigned a value ($1 ="").

Happy New Year to all,
alister
You're right Smilie

Use cut.

Happy new year Smilie
# 6  
Old 12-31-2009
Computer

Here in Paris (France), new year in 10 minutes SmilieSmilieSmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

3. Shell Programming and Scripting

Get rid of awk notation

echo 0.633588 1875 | awk '{print $1 * $2 * 1024}' is there a better way to run the above command? it keeps printing out in notation and i do not want that at all. when i run the above, i get: 1.21649e+06 OS: linux language:bash (1 Reply)
Discussion started by: SkySmart
1 Replies

4. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

5. Shell Programming and Scripting

Get rid of the 7th character of each line if this is a space

I have a text file like this ... B 16 1.340E+05 A 18 3.083E+02 Wu123 1.365E+02 ... I would like to get rid of the 7th character of each line if this is a space character. Thank you, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

6. Shell Programming and Scripting

Getting rid of a field in a big file - Urgent

Hi Unix Gurus, I have a file with 100,000 lines with lines in the format: 1,35518,35518,1,2,1,72,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253888723-g 1253889445 1,78973,78973,1,2,2,91,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253888723-g 1253889445... (3 Replies)
Discussion started by: toms
3 Replies

7. Shell Programming and Scripting

Awk adding a space between integer and the rest of the field

Hi, I have a problem where some of the records I need to process have the first address field as something like "10Walpole Street" where obviously I want it to be "10 Walpole Street". I know I need to somehow separate out the integer and probably form a new string variable, but I just don't... (5 Replies)
Discussion started by: jonathanm
5 Replies

8. Shell Programming and Scripting

How to get rid of extra enter at the end???

Hi guys, I want to automate a few tasks. For one of them, I need to get the output of a command and parse it to extract information I need: drbdadm create-md drbd0 The output is: md_offset 48010952704 al_offset 48010919936 bm_offset 48009453568 Found ext2 filesystem which uses... (2 Replies)
Discussion started by: alirezan
2 Replies

9. UNIX for Dummies Questions & Answers

Multiple field separators in awk? (First a space, then a colon)

How do I deal with extracting a portion of a record when multiple field separators are involved. Let's say I have: Mike Harrington;(555) 555-5555:250:100:175 Christian Dobbins;(555) 555-2358:155:90:201 Susan Dalsass;(555) 555-6279:250:60:50 Archie McNichol;(555) 555-1348:250:100:175 Jody... (3 Replies)
Discussion started by: doubleminus
3 Replies

10. Shell Programming and Scripting

remove space in front or end of each field

Hi, I have a txt file called a.txt which contain over 10,000 records and I would like to remove space before comma or after comma....like below: The input (for example two record 00001,00002): 00001,client,card limited ,02292,N ,162:41 , 192, ... (6 Replies)
Discussion started by: happyv
6 Replies
Login or Register to Ask a Question