user3290216
user3290216

Reputation: 35

Delete line based on column value in unix

I need to cut line(s) from a fixed length file and paste it into new file for the lines that start with 180 and position 35 has "N". I am using ** bash **

**Sample input file:**
070000001                                                                                            075PRMAGDALENA F DEL                                                                                                  180AP997270518411                  Y02092014 
180AP997270518412                  N02092014 
180AP997270518413                  N03212013 
PAYMNT                                                                                               
190EM037490013798700                                                                                 
070000002                                                                                            075PRRERUCHA TROY A                                                                                                    195097130808020800                                                                                   070000003                                                                                            
180AP997270518423                  Y03212013 
**Sample input end**

Code:

while IFS= read -r line
do

## var=$((var+1))
  if [ ${line:0:3} = '180' ] && [ ${line:34:1} = 'N' ]  ;
  then
    echo $line > online_error.txt
  else
    echo $line > online_good.txt
  fi
done <"$file"

Code end

** The output start **

180AP997270518423 N03212013

** Output end ** In the above output the spaces are lost. I would want my output to have similar spacing as my input line. The letter "N" is positioned at 35 in my input file where as in out put its at 19.

The if part writes output to file however I've lost the formatting. I need to keep the original formatting as the output from this script is input for another mainframe program.

Please provide suggestions.

Upvotes: 1

Views: 1120

Answers (2)

Floris
Floris

Reputation: 46375

You could consider the following alternative:

cat b.txt | grep -E '^180.{32}N'  > newFile.txt

To extract the lines that start with 180 and have N in column 35; and

cat b.txt | grep -E -v '^180.{32}N' > remainder.txt

for all the others.

Note - the -E flag is the one that is used on Mac OS for "extended regex". Alternatively you can use egrep - then you don't need the flag. I think this is the same in other versions of Unix.

If you are sure that the letter in the second column is always "the first letter after a space" you could also try

egrep '^180.* N' < b.txt > newFile.txt

and its complement:

egrep -v '^180.* N' < b.txt > newFile.txt

Upvotes: 1

chepner
chepner

Reputation: 531165

Assuming that the statements "position 35 is N" and "the second field starts with N" are equivalent, you could use the awk command

awk '$1 ~ /^180/ && $2 ~ /^N/ {
       print $0 > "online_error.txt";
       next;
     }
     { print $0 > "online_good.txt" }' "$file"

Upvotes: 2

Related Questions