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

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Dan R
  • 45
  • 4
  • You should probably have a look at [How to delete all occurrences of a list of words from a text file?](https://unix.stackexchange.com/q/322310/135943) – Wildcard Aug 06 '18 at 20:01

1 Answers1

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
RalfFriedl
  • 8,816
  • 6
  • 23
  • 34
  • 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 06 '18 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. – don_crissti Aug 06 '18 at 20:10
  • @don_crissti - :) – mikeserv Aug 07 '18 at 02:52
  • @mikeserv - hey Mike, good to see you around again ! – don_crissti Aug 08 '18 at 21:26