1

I have two files: unknown.csv contains

aaa
bbb
ccc

and another file 01.txt as

;lksdjflk aaa lkdsfjdlk
aaa|xxlkjasd|
    |sadkl;k|lsa;dkl

when I define a variable as j=aaa and try

sed "s/${j}//g" 01.txt

it will find occurrences of aaa and remove it. But if I try to read the j from the unknown.csv as

j=$(sed '1q;d' unknown.csv)

and try

sed "s/${j}//g" 01.txt

nothing happens. echo $j for both cases show same result of aaa.

The goal is to read a variable from the unknow.csv file and use it to replace it in 01.txt file.

Thanks

1

1 Answer 1

4

Did you create the file unknown.csv on Windows? If so, it probably contains carriage return characters.

Try

(set -x; echo "$j" )

to show the value of $j.

If you want only line 1 and no '\r' at the end, use

sed -nr '1s/\r?$//p' unknown.csv
4
  • I created it in ubuntu but there is \r carriage character in j and it showed up when I used set -x. I can remove it using j=$(sed -e '1q;d' unknown.csv|sed 's/\r//g'). Is there a better way for that? Thanks
    – Dan R
    Aug 6, 2018 at 20:00
  • 3
    1q;d nonsense aside, it's worth mentioning that working with unknown input requires pre-processing LHS (in this case$j) so as to escape any reserved characters. Aug 6, 2018 at 20:10
  • @don_crissti - :)
    – mikeserv
    Aug 7, 2018 at 2:52
  • @mikeserv - hey Mike, good to see you around again ! Aug 8, 2018 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .