filter out all the records which are having space in the 8th filed of my file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting filter out all the records which are having space in the 8th filed of my file
# 1  
Old 06-25-2009
filter out all the records which are having space in the 8th filed of my file

I have a file which is having fileds separtaed by delimiter.

Ex:

C;4498;qwa;cghy;;;;40;;222122
C;4498;sample;city;;;;34 2;;222123
C;4498;qwe;xcbv;;;;34-2;;222124
C;4498;jj;sffz;;;;41;;222120
C;4498;eert;qwq;;;;34 A;;222125
C;4498;jj;szxzzd;;;;34;;222127



out of these records I want to write those reocrds whcih
have alphanumeric chartacters(ex : space,allphabets,'A','B' etc)
at 8th filed to one file( i.e. filter out all the records which are having space or - as 34 A,34-A in the 8th filed of my file )and remaing to another file.

how to do this using awk or something else in UNIX?

so my final out files should be like this

1st file

C;4498;jj;cghy;;;;40;;222122
C;4498;qwa;sffz;;;;41;;222120
C;4498;jj;szxzzd;;;;34;;222127


2nd file

C;4498;sample;city;;;;34 2;;222123
C;4498;qwe;xcbv;;;;34-2;;222124
C;4498;eert;qwq;;;;34 A;;222125
# 2  
Old 06-25-2009
You can use cut command as follows:
# cut -b8 <filename>

This will return the character at 8th position. Then you can grep for those records which do not match the expression.

Hope this helps :-)
# 3  
Old 06-25-2009
MySQL Hi, It shouold help

#create a file something like 8thfield.txt with below mater
C;4498;qwa;cghy;;;;40;;222122
C;4498;sample;city;;;;34 2;;222123
C;4498;qwe;xcbv;;;;34-2;;222124
C;4498;jj;sffz;;;;41;;222120
C;4498;eert;qwq;;;;34 A;;222125
C;4498;jj;szxzzd;;;;34;;222127

#execute this command to separate the lines which contains 8th field as space or hypen
awk -F";" '{print $8}' 8thfield.txt | egrep " |\-" | xargs -i egrep "{}" 8thfield.txt > file1.txt

#execute this command to ignore the lines which contains 8th field as space or hypen
grep -v "`cat file1.txt`" 8thfield.txt

cat file1.txt
C;4498;sample;city;;;;34 2;;222123
C;4498;qwe;xcbv;;;;34-2;;222124
C;4498;eert;qwq;;;;34 A;;222125

cat file2.txt
C;4498;qwa;cghy;;;;40;;222122
C;4498;jj;sffz;;;;41;;222120
C;4498;jj;szxzzd;;;;34;;222127

Thanks,
Phani Kumar
# 4  
Old 06-25-2009
Code:
 
awk -F";" '$8 !~ /[-A-Z ]/  { print $0 >>"proper.txt";next } { print $0 >>"improper.txt"} ' File_name.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filter records from a log file based on timestamp

Dear Experts, I have a log file that contains a timestamp, I would like to filter record from that file based on timestamp. For example refer below file - cat sample.txt Jan 19 20:51:48 mukul-Vostro-14-3468 systemd: pam_unix(systemd-user:session): session opened for user root by (uid=0)... (6 Replies)
Discussion started by: mukulverma2408
6 Replies

2. Shell Programming and Scripting

Filter duplicate records from csv file with condition on one column

I have csv file with 30, 40 columns Pasting just three column for problem description I want to filter record if column 1 matches CN or DN then, check for values in column 2 if column contain 1235, 1235 then in column 3 values must be sequence of 2345, 2345 and if column 2 contains 6789, 6789... (5 Replies)
Discussion started by: as7951
5 Replies

3. Shell Programming and Scripting

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 Replies

4. Shell Programming and Scripting

Shell script to filter records in a zip file that contains matching columns from another file

Not sure if this is the correct forum for this question. I have two files. file1.zip, file2 Input: file1.zip col1, col2 , col3 a , b , 0:0:0:0:0:c436:9346:d40b x, y, 0:0:0:0:0:880:39f9:c9a7 m, n , 0:0:0:0:0:80c7:9161:fe00 file2.txt col1 c4:36:93:46:d4:0b... (1 Reply)
Discussion started by: anil.v
1 Replies

5. Shell Programming and Scripting

Strange Phenomena with records filed in variable

Trying to find out whether there is a limit for the number of records that can be stored in a variable I set up this small script: #!/usr/bin/ksh for ((i = 1; i < 21; i++)) do n=$(($i*100)) echo "Trying $n records:" recs=$(head -$n error.log) echo "$recs" | wc done ... (9 Replies)
Discussion started by: Cochise
9 Replies

6. UNIX for Dummies Questions & Answers

Filter records in a huge text file from a filter text file

Hi Folks, I have a text file with lots of rows with duplicates in the first column, i want to filter out records based on filter columns in a different filter text file. bash scripting is what i need. Data.txt Name OrderID Quantity Sam 123 300 Jay 342 498 Kev 78 2500 Sam 420 50 Vic 10... (3 Replies)
Discussion started by: tech_frk
3 Replies

7. Shell Programming and Scripting

Filter records based on 2nd file

Hello, I want to filter records of a file if they fall in range associated with a second file. First the chr number (2nd col of 1st file and 1st col of 2nd file) needs to be matched. Then if the 3rd col of the first file falls within any of the ranges specified by the 2nd and 3rd cols , then... (4 Replies)
Discussion started by: ritakadm
4 Replies

8. Shell Programming and Scripting

Apply condition on fixed width file and filter records

Dear members.. I have a fixed width file. Requirement is as below:- 1. Scan each record from this fixed width file 2. Check for value under field no "6" equals to "ABC". If yes, then filter this record into the output file Please suggest a unix command to achieve this, my guess awk might... (6 Replies)
Discussion started by: sureshg_sampat
6 Replies

9. Shell Programming and Scripting

Filter records in a file using AWK

I want to filter records in one of my file using AWK command (or anyother command). I am using the below code awk -F@ '$1=="0003"&&"$2==20100402" print {$0}' $INPUT > $OUTPUT I want to pass the 0003 and 20100402 values through a variable. How can I do this? Any help is much... (1 Reply)
Discussion started by: gpaulose
1 Replies

10. Shell Programming and Scripting

filtering records based on numeric field value in 8th position

I have a ";" delimited file.Whcih conatins a number fileds of length 4 charcters in 8th position But there is a alphanumeric charcters like : space, ";" , "," , "/" , "23-1" , "23 1" , "aqjhdj" , "jun-23" , "APR-04" , "4:00AM" , "-234" , "56784 ", "." , "+" "_" , "&" , "*" , "^" , "%" , "!"... (2 Replies)
Discussion started by: indusri
2 Replies
Login or Register to Ask a Question