Count the pipes "|" in line and delete line if count greter then number.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the pipes "|" in line and delete line if count greter then number.
# 1  
Old 05-09-2016
Linux Count the pipes "|" in line and delete line if count greter then number.

Hello,
I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17".
i.e need to get records having exact 17 pipe separated fields(no more or less)

currently i have below :
Code:
awk -F'|' 'NF!=17 {getline;print $0;}'  < leaseLog.lst > leaseLog_clean.lst

Moderator's Comments:
Mod Comment Please use code tags correctly; don't mix ICODE and CODE !



it will generate the files leaseLog_clean.lst with records having only 17 Pipes but its also containing record having 17+ pipes in line.

Could you please suggest me where i am missing?

Thanks Ketan R

file record example:
Code:
INVALID RECORDS:
0a752000ff3fe00001207355836163|0a7537ca012073558b6163|107.17.55.002|1462686144822|50|28800|1462786596822|1462786934822|878441083|false|1461786144822|1462786140822|0|0|true|2073558b6162|ATAMAW|01042000008402062473558b616209090010118b0401090300
0a752000ff3fe00001207355836163|0a7537ca012073558b6163|107.17.55.002|1462686144822|50|28800|1462786596822|1462786934822|878441083|false|1461786144822|1462786140822
VALID RECORD:
0a1e2000ffffe00001001dd5031b23|0a1e3990010015d6031593|340.30.77.424|1462784144821|50|28800|1462783594821|1462783931821|3344733642|false|1462782344821|1462566144821|0|0|true|001dd6087b92|ATERWF|01042000009002020015d6031b920909000011834401890300


Last edited by RudiC; 05-09-2016 at 08:20 AM.. Reason: Corrected icode to code tag.
# 2  
Old 05-09-2016
There's not a single line in your file having 17 fields:
Code:
awk -F'|' '{print NF}'  file
1
18
12
1
18

To print the lines with exactly 17 fields, try
Code:
awk -F'|' 'NF==17'  file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

4. Shell Programming and Scripting

Find "*.c" and "Makefile" and then delete them with one line

find "*.c" and "Makefile" and then delete them with one line (3 Replies)
Discussion started by: yanglei_fage
3 Replies

5. UNIX for Dummies Questions & Answers

Help! Need "File exist check" and line count

Hello, I've been stuck with this for a few days now :wall:, so I'm hoping someone out there might be kind enough to help me. I need to do a "file exist" check with a variable in the file name. After that I need to do a line count on the file, and output its results to a txt file. For... (3 Replies)
Discussion started by: ronan1219
3 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

8. Shell Programming and Scripting

cat/delete per line any word "192.168.1.12"

Hi All Can u help me.. My problem is delete word per line sample: cat /tmp/file.txt monitor 192.168.1.11 Copying files in current directory 1 monitor 192.168.1.1 Copying files in current directory 2 monitor 192.168.1.12 Copying files in current directory 3 monitor 192.168.1.14... (1 Reply)
Discussion started by: carnegiex
1 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Dummies Questions & Answers

How to count number of occurrences of a "|" from a variable?

I have a variable, var="some1|some2|some3" I want to know how many "|" are in $var. When I say echo $var | grep -c '|' I am getting only 1 :confused: :confused: :confused: ? (4 Replies)
Discussion started by: jingi1234
4 Replies
Login or Register to Ask a Question