awk prints unwanted new lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk prints unwanted new lines
# 1  
Old 11-12-2014
awk prints unwanted new lines

Hello,

I need a little help with the following:

I'm using AWK to read input from a comma-seperated value file, and only printing certain fields like so:

Code:
awk -F "," '{print $1,$3,$6}' /list.csv | tail -1

Which outputs the following:

Code:
server1 APPID OS

I run into a problem when I add it to a for loop:

Code:
LIST=list.csv
for i in $(awk -F "," '{print $1,$3,$6}' ${LIST} | tail -1)
  do
    echo ${i}
done

The output is as follows:

Code:
server1
APPID
OS

How can I fix this so that the output is on one line instead of three different lines?

Thanks in advance.

Last edited by LinuxRacr; 11-12-2014 at 07:37 AM..
# 2  
Old 11-12-2014
Hello LinuxRacr,

Could you please let us know what you are trying to do here. I think we can get the desiered output within awk itself. Kindly let us know the complete requirement. Using awk wihin a loop means each time calling it an may be unnecessarily increasing the execution time for it.
May be we can do things easily within awk itself.

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-12-2014 at 07:56 AM.. Reason: Added a comment line
# 3  
Old 11-12-2014
Your for loop construct reads like this:
Code:
for i in server1 APPID OS
  do
    echo ${i}
done

which it will do:
Code:
echo server1
echo APPID
echo OS

As RavinderSingh says, tell us what you want to get done ...
# 4  
Old 11-12-2014
The csv file is holding data which will be parsed to perform functions such as creating lists, or running system checks based on the info.

Here is a little more of the script:

Code:
LIST=list.csv
OUTDIR=/prod/lists
for i in $(awk -F "," '{print $1,$3,$6}' ${LIST} | tail -1)
  do
    echo ${i}
    SERVER=$(echo "${i}" | awk '{printf $1}')
    APPID=$(echo "${i}" | awk '{printf $2}')
    OS=$(echo "${i}" | awk '{printf $3}')
    APPDIR="${OUTDIR}/${APPID}"
    mkdir -p ${APPDIR}
    echo "${SERVER}" >> ${APPDIR}/${OS}.list
done

# 5  
Old 11-12-2014
Having seen my previous post, you certainly are able to identify the errors in your script...
# 6  
Old 11-12-2014
Use a while loop:
Code:
tail -1 "$LIST" | while IFS=, read SERVER APPID OS APPDIR junk; do
  echo $SERVER
  : ...
done

A for loop will iterate its input one word at a time.

If you don't just want the last line (you did use tail -1), then:
Code:
while IFS=, read SERVER APPID OS APPDIR junk; do
  echo $SERVER
  : ...
done < "$LIST"

This User Gave Thanks to Scott For This Post:
# 7  
Old 11-12-2014
RudiC,

Each line will be different in the csv file being used for input. That is why I am trying to use AWK. Am I missing something?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: BEGIN: prints nothing

My code fails to do anything if I've BEGIN block in it: Run the awk script as: awk -f ~/bin/sum_dupli_gene.awk make_gene_probe.txt #!/usr/bin/awk -f BEGIN { print ARGV #--loads of stuff } END{ #more stuff } (14 Replies)
Discussion started by: genome
14 Replies

2. Shell Programming and Scripting

awk prints only last line

data.txt: NEWTEXTS="frq=63,std=-0.00533584,time=Mar-21-(09:15:03)-2016,epoch=1458576903,avg=64.2059,212.698 frq=197,std=0.587585,time=Mar-21-(09:16:02)-2016,epoch=1458576962,avg=64.2059,483.756 frq=178,std=0.503514,time=Mar-21-(09:46:02)-2016,epoch=1458578762,avg=64.2059,500... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

get rid of unwanted lines

Hi , My output contains the below contents When i execute df -h | grep / | grep -v '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' i get output as below tpshghghg.fgh.com:/vol/scmvol1/scm-vobstore/WebArch.vbs 1.7T 1.3T 452G 74% /vob/WebArch... (6 Replies)
Discussion started by: ptappeta
6 Replies

4. Shell Programming and Scripting

grep out unwanted lines

Hi, I got a log file and I want to grep out a list of unwanted line which are IP's. Basiclly I want everything ecxept the ip's from my list. If I do a while read line do grep -v $ip_from_my_list logfile done <ip_list it just grep's one IP at a time and repeats. :( Thanks for... (3 Replies)
Discussion started by: stinkefisch
3 Replies

5. Shell Programming and Scripting

Remove unwanted lines

I have a .xml file, where i need some output. The xml file is like: Code: <?******?></ddddd><sssss>234</dfdffsdf><sdhjh>534</dfdfa>......... /Code I need the output like: code 234 534 . . . /code How can i do it? (5 Replies)
Discussion started by: anupdas
5 Replies

6. Shell Programming and Scripting

awk system() prints extra 0 -- why?

Here's the command I'm running: # echo "hi" | awk '{etime = system("hostname") ; close("hostname") ; print etime""}' And here's the ouput: server.domain.tld 0 Why in the world is that second line, the one that's just "0", there? Many thanks in advance. (2 Replies)
Discussion started by: treesloth
2 Replies

7. Shell Programming and Scripting

Unwanted field separation in awk

Hi everyone, My problem is strange, I cannot think of why this is happening. I have a set of data that looks like this: Although it does not look it, the fields are tab delimited. I have made sure of this, and awk does recognize them as such. However, it divides what I would expect... (2 Replies)
Discussion started by: ccox85
2 Replies

8. UNIX for Advanced & Expert Users

How to Remove the unwanted Blank Lines

I have a file with the below data, i would like to remove the end blank lines with no data. I used the below commands but could not able to succeed, could you please shed some light. Commands Used: sed '/^$/d' input.txt > output.txt grep -v '^$' input.txt > output.txt input.txt file... (5 Replies)
Discussion started by: Ariean
5 Replies

9. Shell Programming and Scripting

awk / grep / Prints all columns except the first one

I have the a file with the following data. File Content. 1249 snf06.3.0 sw version 1.1 code MD5 192F MD4 MD3 1248 sns06.3.0 sw version 1.1 code MD5 192F MD12 1250 sns06.3.0 sw version 1.1 code MD5 192F0\ MD8 1241 sns06.3.0 sw code MD5 19 1243 sn06.3.0 sw version 1.1 code MD5 19 12... (17 Replies)
Discussion started by: knijjar
17 Replies

10. Shell Programming and Scripting

Filter unwanted lines

Hi All, I have the below input and i only want to filter out some un-wanted info from here. Expected output is below. Can somebody help ? The catch is that i want to grep those lines with term "k=" and lines with term "**" as the 1st column and "07" as the last column. And the number of... (15 Replies)
Discussion started by: Raynon
15 Replies
Login or Register to Ask a Question