0

Script -

#!/bin/bash
NAME=$(cat File1.csv)
grep $NAME File2.csv > Desiredoutput.csv

File1.csv

123

File2.csv

|123|abc|
|:-:|:-:|
|456|xyz|

Desiredoutput.csv

|123|abc|

Actual output - blank :(

Can you please help?

  • `NAME=$(unix2dos – Cyrus Jan 18 '22 at 22:03
  • There may be nonprinting characters in the file (& variable). Run `set -x` to turn on tracing, and try again (then `set +x` to turn tracing off), and see if that indicates anything weird. Also, [`echo $var` is *not* a good way to see what a variable contains](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). – Gordon Davisson Jan 18 '22 at 22:10
  • line 2: unix2dos: command not found @Cyrus – Labeeb Nazim Jan 18 '22 at 22:11
  • @GordonDavisson tried "$NAME", still nothing – Labeeb Nazim Jan 18 '22 at 22:14
  • 1
    My first suggestion was wrong. It should have been called `dos2unix` but I assume that is not installed either. Try this: `NAME=$(tr -d '\r' – Cyrus Jan 18 '22 at 22:22
  • @Cyrus That worked, thank you. Do you know how I could input 123,456 in File1.csv and have the same code work. I dont want to go down the awk route? – Labeeb Nazim Jan 18 '22 at 22:27
  • I suggest to start a new question. – Cyrus Jan 18 '22 at 22:30
  • 2
    @LabeebNazim Read the answers to ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) first. Some deal with DOS/Windows line endings in the script file itself, but there are also some about dealing with line endings in data files (which I think is what you have). – Gordon Davisson Jan 19 '22 at 00:08

0 Answers0