reading a .dat file in to a string

i have folowing code.

i dont want data in an array. i like to put my data file info (after filtering and converting to lower case) in to a string call "name". so it would look like this,

name= ffjtgj345 thgkty3 456gfhf rhtfn4 ......
how do i do that? i dont want to read # or blank lines.

my data file look like this,

ffjtgj345
thgkty3
456gfhf

rhtfn4
#rfrgdbg
3434df

file=../data/globallyIgnoredTc.dat
name=$(echo $(awk '!/#/{print tolower}' $file1))

Hi Scottn, why the echo?

name=$(awk '!/^#/&&NF{printf tolower($0" ")}' ../data/globallyIgnoredTc.dat)
$ echo "$name"
ffjtgj345 thgkty3 456gfhf rhtfn4 3434df
  • or -
name=$(awk '!/^#/&&NF{print tolower($0)}' ../data/globallyIgnoredTc.dat)
  • or -
name=$(egrep -v "^#|^$" ../data/globallyIgnoredTc.dat | tr [:upper:] [:lower:] )
$ echo $name
ffjtgj345 thgkty3 456gfhf rhtfn4 3434df

$ echo "$name"
ffjtgj345
thgkty3
456gfhf
rhtfn4
3434df

The echo was a lazy way of removing blank lines and carriage return when quoting $name, as you did with your first awk

Thanks guys, i will try that.

:slight_smile: I know, I just realized.. I think I'll go to bed early tonight :rolleyes:

i am tring to add values in to my original string. i was doing

name="$name  $new_variable"

but that cause me to print my string as follows,
name=
wfhugh344
djfv343444
.
.
.
uferjt23 <new_variable>

how do i get this new Variable add in to my string so that it will print as follows,

name=
wfhugh344
djfv343444
.
.
.
uferjt23
<new_variable>

Try:

name="$name
$new_variable"

i have following Kshell code. It is working almost perfect. Only issue is, when it writes in to the array, it start with location 1, instead of location 0. for example it will load the array as follows,
ignore[0]=
ignore[1]=wefwf12
ignore[2]=23dfjrtgt
.
.
ignore[n]=erfe3445

How do i get this issue fixed?

for i in $(grep -v "#" ../data/globallyIgnoredTc.dat | awk !/^$/ | awk -F" " '{print NR}' )
do
#echo i =$i
nam_arr[$i]=$(grep -v "#" ../data/globallyIgnoredTc.dat | awk !/^$/ | awk -F " " -v cnter=$i '{if (cnter == NR){print $1}}')
done

i=0
while [[ $i -lt ${#nam_arr[*]} ]] ; do
echo " nam_arr[$i] = ${nam_arr[$i]}"
let i=$i+1
done

Without reading too much into the rest of the code, I would say:

for i in $(grep -v "#" ../data/globallyIgnoredTc.dat | awk !/^$/ | awk -F" " '{print NR-1}' )

Acctually, it didnt do any difference.

my data fine looks like this,
#gfbdnjbdn rfgrfgr
#
#vdfdg erergerg

srgr345 VVDRG56F
455jgg DFERTGvF
.
.
rgbb45 SDFVDDGGB

Odd :slight_smile:

$ cat MyTest
echo Original
grep -v "#" file1 | awk '!/^$/' | awk -F" " '{print NR}'
echo
echo New
grep -v "#" file1 | awk '!/^$/' | awk -F" " '{print NR-1}'
echo
echo For Loop
for i in $(grep -v "#" file1 | awk '!/^$/' | awk -F" " '{print NR - 1}' ); do
  echo $i
done

$ cat file1
#gfbdnjbdn rfgrfgr
#
#vdfdg erergerg

srgr345 VVDRG56F
455jgg DFERTGvF
.
.
rgbb45 SDFVDDGGB


$ ./MyTest
Original
1
2
3
4
5

New
0
1
2
3
4

For Loop
0
1
2
3
4

it is odd. if i send u the data file, do u have a second to run this?

Sure. Send it over (your script too - if not the same as above)