0

I have this function, I'm trying to add "MM" at the end of the line in case the line doesn't contain "MM" and it print it at the start of the line all the time..

recursiveFindReq `pwd`/allReqTemp.txt
while read line; do
        if [[ $line != *MM* ]]; then
                echo $line MM >> temp2
        else
                echo $line >> temp2
        fi
done < allReqTemp.txt

here's the OUTPUT FOR CODE ABOVE OUTPUT FOR CODE ABOVE

and MM BEFORE $LINE MM BEFORE $LINE

and USING PRINTF USING PRINTF

for example: for the input :

07/2018 08:00 09/07/2018 08:01 09:00 150

I expect :

07/2018 08:00 09/07/2018 08:01 09:00 150 MM

but I get :

" MM07/2018 08:00 09/07/2018 08:01 09:00 150"

as you can see in pics

succeed! took me just 2 hours

while read -a line; do
        if [[ ${line[*]} != *MM* ]]; then
                line[6]=MM
                echo ${line[0]} ${line[1]} ${line[2]} ${line[3]} ${line[4]} ${line[5]} ${line[6]} >> temp2
        else
                echo ${line[*]} >> temp2
        fi
done < allReqTemp.txt
Guy Sadoun
  • 427
  • 6
  • 17
  • when you post images, instead of text it's not possible to copy/paste and use for testing the scripts. – karakfa Aug 15 '18 at 17:23
  • 2
    Did you generate your input file on a Windows box? If so, it's possible that each line has a `^M` at the end, which would reset the cursor to the beginning of the line before your script prints `MM`. Try checking the structure of the line endings with `od -c allReqTemp.txt` or `cat -v -e allReqTemp.txt` to make sure. – ghoti Aug 15 '18 at 17:51
  • did you try to remove the end of line char for each $line before processing it? – shaiki siegal Aug 15 '18 at 18:00

2 Answers2

0

not sure what you want to do but perhaps this may provide some guidance.

for the input file

$ cat file
1
2
3MM
4MM
5
6

this will produce

$ awk '{print $0 (/MM$/?"":"MM")}' file

1MM
2MM
3MM
4MM
5MM
6MM

will print the current line ($0) and a suffix if the suffix is not already at the end of the line (/MM$/). Note that this won't allow any white space after the suffix.

karakfa
  • 66,216
  • 7
  • 41
  • 56
0

try to remove the line for each line before processing it:

add this to your code: to remove end of line char:

${line/$'\n'}

   while read line; do
       new_line=${line/$'\n'}
       if [[ $new_line != *MM* ]]; then
            echo $new_line MM >> temp2
       else
            echo $new_line  >> temp2
       fi
   done < file1`
Community
  • 1
  • 1
shaiki siegal
  • 392
  • 3
  • 10