Visit Our UNIX and Linux User Community


Deleting lines in a fixed length file where there is a word at specific location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting lines in a fixed length file where there is a word at specific location
# 1  
Old 09-11-2013
Question Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines.

I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it.

There are two lines as one Header and one trailer which should remain same.

Can somebody help on how to achieve by ksh script or any other way
# 2  
Old 09-11-2013
It you mean character "W" at position 356 try:
Code:
awk 'NR<3; {l=$0; if (substr(l,356,1)=="W") print } END {print l}' infile > outfile

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 09-11-2013
Question It worked

Great it worked.

But somehow the top most line was not included by this command. Any reasons why.

For ex:

Topmost is Header line
Second was the body line but it remains there even if at 356 character it is "1" instead of "W"

Can you please helpSmilie
# 4  
Old 09-11-2013
Quote:
Originally Posted by mohit kanoongo
Great it worked.

But somehow the top most line was not included by this command. Any reasons why.

For ex:

Topmost is Header line
Second was the body line but it remains there even if at 356 character it is "1" instead of "W"

Can you please helpSmilie
In your 1st message in this thread, you said:
Quote:
There are two lines as one Header and one trailer which should remain same.
rdrtx1 and I both understood this to mean that there were two header ilnes and one trailer line. If there is just one header line, change NR<3; in rdtx1's script to NR<2; or NR==1;.

If you are trying this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, on nawk instead of awk.
# 5  
Old 09-11-2013
If $0 is still defined in the END section (which is not necessarily true for all awk implementations), you can try
Code:
awk 'FNR==1; substr($0,356,1)=="W"; END{print}' file

# 6  
Old 09-11-2013
This should with all awk versions
Code:
awk '(NR==1 || substr($0,356,1)=="W"); {l=$0} END{print l}' file


Last edited by MadeInGermany; 09-11-2013 at 10:26 PM.. Reason: bug fix

Previous Thread | Next Thread
Test Your Knowledge in Computers #447
Difficulty: Medium
DebianDog is also Puppy Linux.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

2. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

3. Shell Programming and Scripting

How to find a word and move it a specific location in xml file using perl?

Hi friends, I have one XML file having below structure :- INput XML file :- <?xml version="1.0" encoding="UTF-8"?> <START> <A=value1> <attr name1="a1"> </A> <B=value2> <attr name2="b1"> <attr name3="c1"> </B> </START> output xml file should be === (3 Replies)
Discussion started by: harpal singh
3 Replies

4. Shell Programming and Scripting

Deleting specific lines in a file

Hello, I have a file filled with dates, such as: 04-08-2011 message 04-08-2011 message 03-08-2011 message 01-08-2011 message 31-07-2011 message 24-07-2011 message 15-07-2011 message 13-12-2008 message 26-11-2007 message And I want to delete those lines whose date is older than 10... (5 Replies)
Discussion started by: asanchez
5 Replies

5. Shell Programming and Scripting

Deleting specific lines in a file

Hello, I have a file like this one: 03-07-2011 sunz02 message1 03-07-2011 sunz02 message2 03-07-2011 sunz02 message3 01-07-2011 sunz02 message1 01-07-2011 sunz02 message2 01-07-2011 sunz02 ... (1 Reply)
Discussion started by: asanchez
1 Replies

6. Shell Programming and Scripting

deleting specific lines in a file

I want to delete all lines from a file (orig_file) that contain the regex values (bad_inv_list) I tried a for each loop with sed but it isn't working for file in `cat bad_inv_list`; do sed '/$file/d' orig_file > pared_down_file.1 mv pared_down_file.1 orig_file done I've added... (2 Replies)
Discussion started by: verge
2 Replies

7. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

8. Shell Programming and Scripting

Insert 2 lines in a file at a specific location

Hi, I need to insert two new lines in a file: The file: "..... ...... ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`" .... .... " I need to add the lines: LD_LIBRARY_PATH='$LD_LIBRARY_PATH:$APACHE_HOME/modules' DOWNLOADMODULE_CONF_PATHNAME='$APACHE_HOME/conf/DWLModule.cfg' right... (2 Replies)
Discussion started by: potro
2 Replies

9. Shell Programming and Scripting

Insert lines at specific location in file

Hi There I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket) ... (17 Replies)
Discussion started by: hcclnoodles
17 Replies

10. Shell Programming and Scripting

Deleting specific lines in a file

I have a file which has some lines starting with a particular word. I would like to delete 5 lines before each such line containing that particular word. eg: line1 line2 line3 line4 line5 line6 "particular word"... I would like to delete line2-line6 and all such occurences in that... (4 Replies)
Discussion started by: ramu_1980
4 Replies

Featured Tech Videos