How to search for blank fields in a text file from a certain position?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search for blank fields in a text file from a certain position?
# 1  
Old 03-28-2014
How to search for blank fields in a text file from a certain position?

Sample txt file :

Code:
OK00001111112|
OK00003443434|skjdaskldj
OK32812983918|asidisoado
OK00000000001|
ZM02910291029|sldkjaslkjdasldjk

what would be the shell script to figure out the blank space (if any) after the pipe sign?

Last edited by Don Cragun; 03-28-2014 at 06:04 AM.. Reason: Add CODE tags.
# 2  
Old 03-28-2014
Your sample input doesn't appear to have any spaces or tabs after the vertical bars. But, the code:
Code:
awk -F '|' '$2 ~ /[[:blank:]]/' file.txt

when file.txt contains:
Code:
OK00001111112|
OK00003443434|skjdaskldj
OK32812983918|asidisoado
OK00000000001|
ZM02910291029|sldkjaslkjdasldjk
abc| space
def|	tab
ghi|12 34space
jkl|56	78tab

produces the output:
Code:
abc| space
def|	tab
ghi|12 34space
jkl|56	78tab

Is this what you were trying to do?
# 3  
Old 03-28-2014
yeah, I noticed it now that there is no blank space after the | in sample.txt file.

I wish to print the rows which have only one column (ie. missing 2nd column after | )

output should look like :
Code:
Blank Records found : 
OK00001111112| 
OK00000000001|


Last edited by Franklin52; 03-28-2014 at 06:35 AM.. Reason: Please use code tags
# 4  
Old 03-28-2014
There is a big difference between "blank" and "empty". To print lines where field 2 is empty, try:
Code:
awk -F '|' '$2==""' file.txt

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 03-28-2014
Thanks a Ton Don!.. It worked..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

4. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

5. UNIX for Dummies Questions & Answers

How to find position of blank row in UNIX?

Hi I have file "emp.txt"like below Emp Id Name 123 aa 123 bb 223 cc 233 dd 334 ee Please help me to know that the position of the blank row. (5 Replies)
Discussion started by: rock_01
5 Replies

6. Shell Programming and Scripting

Search string at a particular position in a file

Hi, i have a text file as : abc 0 1 Pass hjk 1 1 Pass bhk 0 0 Fail jjh 8 2 Pass nkji 0 1 Pass Now I want to check that if 1st column is jjh , then , store the value of 3rd string of that line in a variable. Hence, 2... (8 Replies)
Discussion started by: Anamika08
8 Replies

7. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

8. Shell Programming and Scripting

Fixed width file search based on position value

Hi, I am unable to find the right option to extract the data in the fixed width file. sample data abcd1234xgyhsyshijfkfk hujk9876 io xgla loki8787eljuwoejroiweo dkfj9098 dja Search based on position 8-9="xg" and print the entire row output ... (4 Replies)
Discussion started by: onesuri
4 Replies

9. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

10. Shell Programming and Scripting

search fields within a file

I need to be able to search multiple fields within a file that contain blank (nothing). Not sure what command to use? thought it would be grep but not sure how to write it The reason is i have a script that when it does this and identifies a field which has a blank it errors out of the script... (4 Replies)
Discussion started by: Pablo_beezo
4 Replies
Login or Register to Ask a Question