Home > Software engineering >  Picking random values from two files
Picking random values from two files

Time:01-06

Any idea why I can't print FNAME and LNAME variables on the same line? I've run out of ideas for this basic test.

for f in {1}; do
  FNAME=$(shuf -n 1 fnames.csv)
  echo $FNAME
  LNAME=$(shuf -n 1 lnames.csv)
  echo $LNAME
  echo
  echo $FNAME $LNAME
done

The output in terminal shows this

joe
bloggs

 bloggs

When I'm expecting it to print this

joe bloggs

CodePudding user response:

Your input files contain windows/dos line endings (\r\n).

Consider:

$ cat fnames.csv
joe

$ cat lnames.csv
bloggs

$ unix2dos ?names.csv

$ od -c fnames.csv
0000000   j   o   e  \r  \n
0000005

$ od -c lnames.csv
0000000   b   l  o   g   g   s  \r  \n
0000008

Notice the od -c output contains \r followed by \n; this is a windows/dos line ending.

With the \r entries in the files OP's code will generate the following:

joe
bloggs

 bloggs

OP can remove the \r characters via several methods (google and/or stackoverflow searches will bring up several hits), eg:

$ dos2unix ?names.csv

$ od -c fnames.csv
0000000   j   o   e  \n
0000004

$ od -c lnames.csv
0000000   b   l   o   g   g   s  \n
0000007

Now OP's code will generate:

joe
bloggs

joe bloggs

As for OP's requirement the output shows up with camelcase (Joe Bloggs) ... see the link from Nic3500's comment

  •  Tags:  
  • Related