Creating variable using awk in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating variable using awk in bash
# 1  
Old 03-17-2010
Creating variable using awk in bash

I would like to create a variable within my bash script using awk. I'm reading in a line from an external file, then outputting to a new file in a specific format. But, it doesnt quite work as I have expected and could use some help.

(A pertinent excerpt of ) the bash code is:
Code:
      count=1
      stationlist=$1
      lng=$(sed -n "${count}p" $stationlist | awk -F, '{ print $1 }' )
      lat=$(sed -n "${count}p" $stationlist | awk -F, '{ print $2 }' )
      sname=$(sed -n "${count}p" $stationlist | awk -F, '{ print $3, $4 }' )
      echo "sname=\"$sname\" " >> points.xml

The file "stationlist" looks like
-94.998097,29.713967,WEST MAIN STREET,SH 146 NB

I want the output to be
sname="WEST MAIN STREET SH 146 NB"

but unfortunately it is
name="WEST MAIN STREET SH 146 NB

Curiously, if I instead replace line 5 above with
Code:
sname=$(sed -n "${count}p" $stationlist | awk -F, '{ print $3 }' )

then I get
sname="WEST MAIN STREET"
which is almost what I want.

Would appreciate some help, thank you!
# 2  
Old 03-17-2010
Question

What are you trying to accomplish with the sed -n
-- are you merely trying to read record (line) by record?

Is your issue that you are missing a final " character
# 3  
Old 03-17-2010
Quote:
Originally Posted by snoitacilppa
...
The file "stationlist" looks like
-94.998097,29.713967,WEST MAIN STREET,SH 146 NB

I want the output to be
sname="WEST MAIN STREET SH 146 NB"

but unfortunately it is
name="WEST MAIN STREET SH 146 NB

...
Ok, here's a hunch. If the name of the input file (1st input parameter) is actually "stationlist" and if that line (beginning with -94...) is the first line in there, then run this command:

Code:
head -1 stationlist | od -bc

Chances are, your output would look like this -

Code:
$
$ head -1 stationlist | od -bc
0000000 055 071 064 056 071 071 070 060 071 067 054 062 071 056 067 061
          -   9   4   .   9   9   8   0   9   7   ,   2   9   .   7   1
0000020 063 071 066 067 054 127 105 123 124 040 115 101 111 116 040 123
          3   9   6   7   ,   W   E   S   T       M   A   I   N       S
0000040 124 122 105 105 124 054 123 110 040 061 064 066 040 116 102 015
          T   R   E   E   T   ,   S   H       1   4   6       N   B  \r
0000060 012
         \n
0000061
$

You will have to remove that "\r" from the file.
Do this -

Code:
dos2unix stationlist stationlist

If that doesn't work i.e. if "dos2unix" isn't present in your system, try "dos2ux". If that doesn't work either, then try the following 2 commands -

Code:
tr -d '\r' <stationlist >my_temp_file
mv my_temp_file stationlist

(I've assumed you don't already have a file called "my_temp_file" in your current directory.)

Thereafter, if the output of "od -bc" doesn't show the "\r" character, then your awk stuff should work -

Code:
$
$ head -1 stationlist | od -bc
0000000 055 071 064 056 071 071 070 060 071 067 054 062 071 056 067 061
          -   9   4   .   9   9   8   0   9   7   ,   2   9   .   7   1
0000020 063 071 066 067 054 127 105 123 124 040 115 101 111 116 040 123
          3   9   6   7   ,   W   E   S   T       M   A   I   N       S
0000040 124 122 105 105 124 054 123 110 040 061 064 066 040 116 102 012
          T   R   E   E   T   ,   S   H       1   4   6       N   B  \n
0000060
$

HTH,
tyler_durden
# 4  
Old 03-23-2010
that's it!

Indeed the issue was the \r and your dos2unix command did the trick. Thank you!
# 5  
Old 03-23-2010
Glad to know that.

Cheers!
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable expansion in awk script

Hello, I need to split a file into two of different locations by re-direction in awk. cat infle aaa 1 3 bbb 2 4 aaa 3 3 bbb 4 4 aaa 5 3 bbb 6 4 cat /storage/tmp/group_a.gtf aaa 1 3 aaa 3 3 aaa 5 3 cat /storage/tmp/group_b.gtf bbb 2 4 bbb ... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Shell Programming and Scripting

Problem with awk instructions and bash variable

Hi everyone, I'm trying to write a small script to automatize row data treatment. However, I got some trouble with the awk command. I want to use awk to extract a define paragraph from a text file. The first and final lines are defined externally in two variables called debut and fin. I... (2 Replies)
Discussion started by: TeaTimeSF
2 Replies

3. Shell Programming and Scripting

Creating bash array name from variable

Hi gurus, I need to create arrays from variables, via a loop. The issue I have is with the array name creation. How do I use a variable to define an array? I want to do something like declare -a $H where $H is my loop variable. I then need to add items to each array I've created,... (3 Replies)
Discussion started by: melias
3 Replies

4. Shell Programming and Scripting

awk not escape my bash variable

I tried to parse data from switch configuration files vlan 1727 name SQ5506-15 by port tagged ethe 8/1 to 8/2 untagged ethe 1/13 ! vlan 2105 name SQ5620-7007(BR2) by port tagged ethe 8/1 to 8/2 untagged ethe 1/17 ! interface ethernet 1/13 port-name SQ5506-15.nic0 rate-limit... (2 Replies)
Discussion started by: winggundamth
2 Replies

5. Shell Programming and Scripting

populate a bash variable from an awk operation

Hi, I'm trying to populate bash script variable, data_size with the size of the largest file in my current directory data_size=$(ls -lS | grep -v "total" | head -1) | awk '{ print $5 }' I've tried adding an echo before the call to awk data_size=$(ls -l | grep -v "total" | head -1) |... (2 Replies)
Discussion started by: mark_s_g
2 Replies

6. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

7. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

8. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

Bash and Awk for creating directories and moving files

I have a security system that FTPs the camera files to my machine, however I want to sort the pictures (taken every 30s) into directories by hour. Every picture uses the following file format. yymmddhhmmsstt.jpg (where tt is the milliseconds) I am thinking the for loop is best for file... (11 Replies)
Discussion started by: Kiint
11 Replies

10. Shell Programming and Scripting

bash/awk scripting help (creating OLD new users)

I need some help making this script... I guess I'm having trouble even interpretating what to even get started on... I need to create a script that will search a given directory (typically a user's home directory, but not necessarily) as provided on the command line and any sub-directors for... (2 Replies)
Discussion started by: Jukai
2 Replies
Login or Register to Ask a Question