Remove lines in a positional file based on string value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove lines in a positional file based on string value
# 1  
Old 05-02-2013
Computer Remove lines in a positional file based on string value

Gurus,
I am relatively new to Unix scripting and am struck with a problem in my script. I have positional input file which has a FLAG indicator in at position 11 in every record of the file.

If the Flag has value =Y, then the record from the input needs to be written to a new file.However if the FLAG has value ="N" the record needs to be ignored

Below is the logic, sample input and expected output that i am trying to unsuccessfully implement. Any help/pointers to resolve my issue will be greatly appreciated

Regards,
Gsam
Code:
filename=$1

while read mline; do
    C_Offadd=`awk '{val=substr($mline,11,1);print val}'`
    if [ "${C_Offadd}" -eq "Y" ]; then
      echo $mline >> $filename.rew
    fi
done<$filename

Sample Input
DTLDETAIL1YAPPLE
DTLDETAIL2YBALL
DTLDETAIL3NCAT
DTLDETAIL4NDOG
DTLDETAIL5YFISH

Expected Output
DTLDETAIL1YAPPLE
DTLDETAIL2YBALL
DTLDETAIL5YFISH
# 2  
Old 05-02-2013
No need for a while loop to read line by line. You can achieve this by using below awk code:
Code:
awk 'substr($0,11,1)=="Y"' input > output

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-02-2013
Perhaps a simpler approach?

Code:
$ cat sample31.txt
DTLDETAIL1YAPPLE
DTLDETAIL2YBALL
DTLDETAIL3NCAT
DTLDETAIL4NDOG
DTLDETAIL5YFISH

$ awk '{if (substr($0,11,1)=="Y") print $0}' <sample31.txt
DTLDETAIL1YAPPLE
DTLDETAIL2YBALL
DTLDETAIL5YFISH

This User Gave Thanks to joeyg For This Post:
# 4  
Old 05-02-2013
Thanks Yoda & joeyg it worked like a charm ! You guys were fast & awesome Smilie !

Best Regards,
Gsam
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove lines from File.A based on criteria in File.B

Hello, I have two files of the following form. I would like to remove from File.A where the first three colum matches values in File.B to give the output in File.C File.A 121 54321 PQR CAT 122 765431 ABC DOG 124 98765 ZXY TIGER 125 86432 GEF LION File.B 122 765431 ABC 125 86432 GEF... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

2. Shell Programming and Scripting

Parsing a file based on positional constraints

I have a list file1 like dog cow fox cat fish duck crowI want to classify the elements of file1 based on constrains applied on file2. Additionally the number of elements (words) in the each line of file2 is not fixed. This is my file2 cow cat fox dog cow fox dog fish crow fox dog cat ... (5 Replies)
Discussion started by: sammy777
5 Replies

3. Shell Programming and Scripting

Remove duplicate lines from file based on fields

Dear community, I have to remove duplicate lines from a file contains a very big ammount of rows (milions?) based on 1st and 3rd columns The data are like this: Region 23/11/2014 09:11:36 41752 Medio 23/11/2014 03:11:38 4132 Info 23/11/2014 05:11:09 4323... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

4. Shell Programming and Scripting

Two files, remove lines from second based on lines in first

I have two files, a keepout.txt and a database.csv. They're unsorted, but could be sorted. keepout: user1 buser3 anuser19 notheruser27 database: user1,2343,"information about",field,blah,34 user2,4231,"mo info",etc,stuff,43 notheruser27,4344,"hiya",thing,more thing,423... (4 Replies)
Discussion started by: esoffron
4 Replies

5. Shell Programming and Scripting

Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format: I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one. Any help would be greatly appreciated. (10 Replies)
Discussion started by: nwalsh88
10 Replies

6. Shell Programming and Scripting

Remove lines based on column value

Hi All, I just need a quick fix here. I need to delete all lines containing "." in the 6th column. Input: 1 1055498 . G T 5.46 . 1 1902377 . C T 7.80 . 1 1031540 . A G 34.01 PASS 1 ... (2 Replies)
Discussion started by: Hkins552
2 Replies

7. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

8. Shell Programming and Scripting

Remove lines from XML based on condition

Hi, I need to remove some lines from an XML file is the value within a tag is empty. Imagine this scenario, <acd><acdID>2</acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> I... (3 Replies)
Discussion started by: giles.cardew
3 Replies

9. Shell Programming and Scripting

Remove lines based on contents of another file

So, this issue is driving me nuts! I was hoping to get a lending hand here... I have 2 files: file1.txt contains: this is example1 this is example2 this is example3 this is example4 this is example5 file2.txt contains: example3 example5 Basically, I need a script or command to... (4 Replies)
Discussion started by: bashshadow1979
4 Replies

10. Shell Programming and Scripting

remove lines based on score criteria

Hi guys, Please guide for Solution. PART-I INPUT FILE (has 2 columns ID and score) TC5584_1 93.9 DV161411_2 79.5 BP132435_5 46.8 EB682112_1 34.7 BP132435_4 29.5 TC13860_2 10.1 OUTPUT FILE (It shudn't contain the line ' BP132435_4 29.5 ' as BP132435 is repeated... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies
Login or Register to Ask a Question