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:
awk -F "," '{print $1,$3,$6}' /list.csv | tail -1
Which outputs the following:
server1 APPID OS
I run into a problem when I add it to a for loop:
LIST=list.csv
for i in $(awk -F "," '{print $1,$3,$6}' ${LIST} | tail -1)
do
echo ${i}
done
The output is as follows:
server1
APPID
OS
How can I fix this so that the output is on one line instead of three different lines?
Thanks in advance.
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
RudiC
November 12, 2014, 6:54am
3
Your for loop construct reads like this:
for i in server1 APPID OS
do
echo ${i}
done
which it will do:
echo server1
echo APPID
echo OS
As RavinderSingh says, tell us what you want to get done ...
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:
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
RudiC
November 12, 2014, 7:07am
5
Having seen my previous post, you certainly are able to identify the errors in your script...
Scott
November 12, 2014, 7:10am
6
Use a while loop:
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:
while IFS=, read SERVER APPID OS APPDIR junk; do
echo $SERVER
: ...
done < "$LIST"
1 Like
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?
Hello LinuxRacr,
For simple printing following may help (As per post#1).
awk 'END{print "SERVER= " $1 OFS "APPID= "$3 OFS "OS= "$6}' Input_file
For getting values into variables we can use following.
SERVER=`awk 'END{print $1}' Input_file`
APPID=`awk 'END{print $3}' Input_file`
OS=`awk 'END{print $6}' Input_file`
After that you try to write the script and do the further more operations as you have shown us to create directory etc as per your requirements.
Hope this helps.
Thanks,
R. Singh
Great Scott! I believe my issue is solved. IFS did the trick.