Find text line1 in spreadsheet and get column 2

2019-08-31 07:09发布

I'm trying to show the field next to the name that I find with awk command in spredsheet.xls

I have this situation;

  **Column1**               *Column2* 

  **PIPPO**    *pippo@gmail.com darth@gmail.com*
  **PLUTO**    *pluto@gmail.com duck@gmail.com*
  **WATER**    *drink@gmail.com water@gmail.com*

how do I search for a word in column 1 and display the contents in column 2?

awk '$1 == "PIPPO"' spreadsheet.xls

this command show:

PIPPO    pippo@gmail.com darth@gmail.com

Update

this is my code ---- Column1 (PIPPO, PLUTO, WATER) and awkvar is the same thing----

for fullname in /*.zip; do 
filename="${fullname##*/}" 
awkvar=$(echo $filename | cut -d_ -f2-2) 
awk -v var="$awkvar" '{print $2,$3}' spreadsheet.xls 
done 

but this not work. How do I put a pattern variable in awk?

Update 2

i have update list xls with comma separated

FIELD1,pippo@gmail.com,darth@gmail.com
FIELD1,pippo@gmail.com,darth@gmail.com,sampei@gmail.com

output=(awk -F ',' -v var="$awkvar" '$1==var {print $2,$3,$4} spreadsheet.txt)

but awk delete some letters at the beginning of the output

echo "$output"
  • ippo@gmail.com darth@gmail.com
  • pippo@gmail.com darth@gmail.com sampei@gmail.com

but if i remove $4

output=(awk -F ',' -v var="$awkvar" '$1==var {print $2,$3} spreadsheet.txt)

echo "$output"

it is correct display for line1 but not for line2:

  • pippo@gmail.com darth@gmail.com
  • pippo@gmail.com darth@gmail.com

What am I doing wrong?

2条回答
一夜七次
2楼-- · 2019-08-31 07:38

Probably you want to display only 2nd column. Use this awk:

awk '$1 == "PIPPO" {print $2}' spreadsheet.xls

EDIT:

If you want to display all columns after 1st columns then use:

sed -n 's/^PIPPO *//p' spreadsheet.xls
pippo@gmail.com darth@gmail.com
查看更多
在下西门庆
3楼-- · 2019-08-31 07:44

You can use the following:

awk '$1=="PIPPO" {print $2}' file

That will output:

pippo@gmail.com

The format is not clear to me, so depending on how it is you may need {print $2, $3}.


As said in comments, based on your updated code you can use this to use the bash var inside awk:

awk -v var="$awkvar" '$1==var {print $2,$3}' spreadsheet.xls
查看更多
登录 后发表回答