Q) How to remove the lines which are same as the first line.
Awk command can be used to remove the lines which are same as the first line in a file. I will also show you another method of removing the file. As an example, consider the file with the below data.
The first line contains the text "Header". We need to remove the lines which has the same text as the first line.
The required output data is
The awk command can be used to achieve this. The awk command for this is
The other way to get the output is using bash script.
Awk command can be used to remove the lines which are same as the first line in a file. I will also show you another method of removing the file. As an example, consider the file with the below data.
Header line2 line3 Header line5 line6 line7 Header
The first line contains the text "Header". We need to remove the lines which has the same text as the first line.
The required output data is
Header line2 line3 line5 line6 line7
The awk command can be used to achieve this. The awk command for this is
awk '{
if(NR==1)
{
x=$0;
print $0
}
else if(x!=$0)
print $0
}' file.txt
The other way to get the output is using bash script.
x=`head -1 file.txt`; echo $x; cat file.txt | grep -v \^"${x}"\$
Post a Comment