Link to home
Start Free TrialLog in
Avatar of Jasmin shahrzad
Jasmin shahrzad

asked on

read a file via sed , awk or perl

how to do the following from command line:
read a file start file is like that
scan id 123
difference
only 2 line then check if the second line is difference then variable scan in second file is 123

if start file like
scan id 123
no difference do nothing.
ASKER CERTIFIED SOLUTION
Avatar of MURUGESAN N
MURUGESAN N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Similar and updated command:
for InputFiles in InputFile_29006828.txt Input_SecondFile.txt Input_ThirdFile.txt
do
        ValidateFirstSecondLine=''`/bin/awk '{
                arr[NR]=$0;
                LastLineNum=NR
        }
        END {
                if ( arr[1] != arr[2])
                {
                        if ( 1 >= LastLineNum)
                        {
                                printf( "File %s having only %d line\n", FILENAME, LastLineNum);
                        }
                        else
                        {
                                printf( "First line and second line varies in %s\n", FILENAME);
                        }
                }
        }'   "$InputFiles" | /usr/bin/tr -d "\n"`''
        if test "" != "$ValidateFirstSecondLine"
        then
                echo "$ValidateFirstSecondLine"
                echo Press Enter
                read
        fi
done

Open in new window

Output:
First line and second line varies in InputFile_29006828.txt
Press Enter

File Input_SecondFile.txt having only 1 line
Press Enter

Open in new window

1. Provided related and tested answer
2. Question being inactive.