I am trying to do some math on 2nd column of a txt file , but some lines are not numbers , i only want to operate on the lines which have numbers .and keep other line unchanged
txt file like below
aaaaa 
1 2
3 4
How can I do this?
I am trying to do some math on 2nd column of a txt file , but some lines are not numbers , i only want to operate on the lines which have numbers .and keep other line unchanged
txt file like below
aaaaa 
1 2
3 4
How can I do this?
 
    
     
    
    Doubling the second column in any line that doesn't contain any alphabetic content might look a bit like the following in native bash:
#!/bin/bash
# iterate over lines in input file
while IFS= read -r line; do
  if [[ $line = *[[:alpha:]]* ]]; then
    # line contains letters; emit unmodified
    printf '%s\n' "$line"
  else
    # break into a variable for the first word, one for the second, one for the rest
    read -r first second rest <<<"$line"
    if [[ $second ]]; then
      # we extracted a second word: emit it, doubled, between the first word and the rest
      printf '%s\n' "$first $(( second * 2 )) $rest"
    else
      # no second word: just emit the whole line unmodified
      printf '%s\n' "$line"
    fi
  fi
done
This reads from stdin and writes to stdout, so usage is something like:
./yourscript <infile >outfile
 
    
    thanks all ,this is my second time to use this website ,i find it is so helpful that it can get the answer very quickly
I also find a answer below
#!/bin/bash
FILE=$1
while read f1 f2 ;do 
if[[$f1 != *[!0-9]*]];then 
  f2=`echo "$f2 -1"|bc` ;
  echo "$f1 $f2"
else
  echo "$f1 $f2"
fi
done< %FILE
