filnding the line in afile which has less number of '|''s


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers filnding the line in afile which has less number of '|''s
# 8  
Old 01-25-2012
Actually if there are n "|"s then NF will be n+1.
So,if you have 192 "|"s then NF=193.
So convert your awk one liner accordingly.

---------- Post updated at 06:19 PM ---------- Previous update was at 05:43 PM ----------

hi,

I have tested just similar to your scenario:
My input file:
Code:
pandeeswaran@ubuntu:~/training$ cat testfile
aghagh|hdjhdkhak|hdjhadja|hdsjhdj|hhjdh|hsjdhsk
gdhdg|hdjah|sklksl
hshsjh|hdjhdjh|dsjhdjsh|hjsdhs|hdjshdjs|ssk
jdkjkd|jskjks
jkj|jksjk

In the above data, i want only the lines which has less than 5 "|"symbols and the below ask does that:
Code:
pandeeswaran@ubuntu:~/training$ nawk -F"|" 'NF<6{print}' testfile
gdhdg|hdjah|sklksl
jdkjkd|jskjks
jkj|jksjk

# 9  
Old 01-25-2012
Just for interest, a Shell method.

Code:
cat filename.txt | while read line
do
        # Delete everything except pipe. Count the result.
        count=`echo "${line}" | tr -dc '|' | wc -c`
        if [ ! ${count} -ge 192 ]
        then
                echo "${line}"
        fi
done

awk programs will be faster for large files.

Last edited by methyl; 01-25-2012 at 08:58 AM.. Reason: typos
# 10  
Old 01-25-2012
And one more pure bash based solution:
Code:
pandeeswaran@ubuntu:~/training$ cat findpipe.sh
#!/usr/bin/bash
while read line
do
a=${#line}
#echo $a
line1=`echo $line|tr -d "|"`
b=${#line1}
#echo $line1
diff=$(( $a - $b ))
if [ $diff -lt  5 ]
then
echo $line
else
continue
fi
done < testfile


pandeeswaran@ubuntu:~/training$ bash findpipe.sh
gdhdg|hdjah|sklksl
jdkjkd|jskjks
jkj|jksjk

---------- Post updated at 07:27 PM ---------- Previous update was at 06:35 PM ----------

Shorter one:
Code:
while read line
do
       [[ `echo "${line}" | tr -dc '|' | wc -c` -lt 192 ]] && echo $line
done<input_file

This will display the lines which contains less than 192 "|" symbols.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

2. Shell Programming and Scripting

Cut from specific line number to a line number

Hi All, I've a file like this.. Sheet1 a,1 a,2 a,3 a,4 a,5 Sheet2 a,6 a,7 a,8 a,9 a,10 Sheet3 a,11 a,12 a,13 (7 Replies)
Discussion started by: manab86
7 Replies

3. UNIX for Dummies Questions & Answers

how to join all lines in afile in unix

Hi, I have a unix file which has many lines, i need to join all the lines to single line. Eg: myfile.txt contains: a 123 45fg try and i need the output as : a 123 45fg try Please help me on this. Thanks! (2 Replies)
Discussion started by: RP09
2 Replies

4. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. Shell Programming and Scripting

delete lines containing a specific word in afile

Hi, Please suggest how to write a shell script which delets all the lines containing the word unix in the files supplied as argument in the shell. (4 Replies)
Discussion started by: sireesha9
4 Replies

7. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

8. Shell Programming and Scripting

how many times a word occur in afile

i want a shell script program for how many times a word occur in a file. i need not the line number but i want the counts of the particular word for eg:- hai how r u.. i am from andhra pradesh.. i am from tenali.i need this answer.i need it urgently.. i hope u will answer this ... ... (9 Replies)
Discussion started by: madhu.it
9 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question