0

in a cygwin environment i want to read a csv line for line, and try to get the values from two columns. So i have

 while read line ; do echo `cut -d";" -f5`; done < allk.lst

and the right values are shown.

But:

while read line ; do echo `cut -d";" -f5`; echo `cut -d";" -f4`; done < allk.lst

again shows the values as before...

Any hints to show both values?

Thanks, Bommel

bommel
  • 1
  • 1
  • I guess `echo \`cut -d";" -f5,4\`;` will give you what you expect. Anyway, it seems you have to read about stream redirection in shell. What your script is doing actually is probably not what you think it will do. – Zilog80 Apr 22 '21 at 13:07

2 Answers2

0
  1. cut -f accepts a list of fields, so there no need to call cut twice

    Using cut command to remove multiple columns

  2. echo cut -d";" -f5; does not do what you'd expect. At first, the variable line is missing.

After applying those fixes, you command would look something like:

while read line; do echo $line | cut -d";" -f4-5 ; done < test.txt

Try a demo online!

0stone0
  • 34,288
  • 4
  • 39
  • 64
0

Hmmm, thanks at first.

Some curiosity:

When using

 $ time while read line; do echo $line | cut -d";" -f4-5 ; done < allk.txt
K700W1666;S728A0103
K700W1651;S727A7570
K700W1654;S727A7579
K700W1657;S727A7581
K700W1660;S727A7582
K700W3040;S728A0099
K700W3043;S728A0107
K700W3042;S732A4280
K700W3594;S724A5213
K700W3600;S727A7609
K700W3603;S727A7615
K700W3597;S727A7617
K700W3604;S727A7589
K700W3624;S728A1599
K700W2164;S728A0091
K700W2165;S728A0110
K700W3565;S727A7577
K700W3568;S727A7578
K700W3560;S725A4806
K700W3563;S725A8285
K700W3559;S726A1925
K700W3562;S728A0197
K700W2016;S726A1929
K700W2012;S725A5172
K700W2015;S728A0056
K700W2014;S728A0061
K700W2017;S728A0067

real    0m12.165s
user    0m0.482s
sys     0m1.390s

it takes 12 seconds, and there is a semicolon between.

Whereas

$ time while read line; do echo `cut -d";" -f4-5` ; done < allk.txt
K700W1651;S727A7570 K700W1654;S727A7579 K700W1657;S727A7581 K700W1660;S727A7582 K700W3040;S728A0099 K700W3043;S728A0107 K700W3042;S732A4280 K700W3594;S724A5213 K700W3600;S727A7609 K700W3603;S727A7615 K700W3597;S727A7617 K700W3604;S727A7589 K700W3624;S728A1599 K700W2164;S728A0091 K700W2165;S728A0110 K700W3565;S727A7577 K700W3568;S727A7578 K700W3560;S725A4806 K700W3563;S725A8285 K700W3559;S726A1925 K700W3562;S728A0197 K700W2016;S726A1929 K700W2012;S725A5172 K700W2015;S728A0056 K700W2014;S728A0061 K700W2017;S728A0067

real    0m0.308s
user    0m0.015s
sys     0m0.030s

takes only 0.3 sec... but also with a semic. So: what is the best way to read this values in two variables (for building SQL commands)?

bommel
  • 1
  • 1
  • It's often not welcome here to post a question as an answer; perhaps a new question would be more rewarding. – Armali Apr 22 '21 at 15:15