Validation of character separated lines in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Validation of character separated lines in a file
# 1  
Old 05-27-2008
Validation of character separated lines in a file

Hi,

I have a file with "|" separated fields. If the line doesn't contain n "|" (say 2), then put this line in a file called invalid_file.txt. If it does put this row in a file called valid_file.txt.

For e.g. A file contain following rows:
Code:
Hi|Hello
How|Are|You
Hello

then
invalid_file.txt should contain:
Code:
How|Are|You
Hello

valid_file.txt should contain:
Code:
Hi|Hello

Any help is highly appreciated.


Thanks,
SK

Last edited by Yogesh Sawant; 05-27-2008 at 08:08 AM.. Reason: added code tags
# 2  
Old 05-27-2008
I have prepared the shell script. Thanks.

Pls see the shell script:

Code:
TestValid()
{
while read -r line
do
echo ${line} | awk -F"|" '{
if (NF == sep_num)
   printf("%s\n", $0) >> fname"_valid_rec";
else
   printf("%s\n", $0) >> fname"_invalid_rec"
}' sep_num="${2}" fname="${1}"
done < ${1}
}

while read -r col1 col2
do
if [ -f ${col1}"_valid_rec" ]; then
  rm ${col1}"_valid_rec"
fi
if [ -f ${col1}"_invalid_rec" ]; then
  rm ${col1}"_invalid_rec"
fi
TestValid ${col1} ${col2}
if [ -f ${col1}"_invalid_rec" ]; then
   mv ${col1} ${col1}"_org"
fi
if [ -f ${col1}"_valid_rec" ]; then
   mv ${col1}"_valid_rec" ${col1}
fi
done < para_new.cfg


Last edited by Yogesh Sawant; 05-27-2008 at 08:09 AM.. Reason: added code tags
# 3  
Old 05-27-2008
With awk you can do this with a oneliner:

Code:
awk 'BEGIN{FS=OFS="|"}NF==2{print $0 > "valid_file.txt";next}{print $0 > "invalid_file.txt"}' file

Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.
# 4  
Old 05-27-2008
You could have used awk:
Code:
awk -F "|" 'NF!=2' file > output

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

2. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

3. Shell Programming and Scripting

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

4. Shell Programming and Scripting

Merging two special character separated files based on pattern matching

Hi. I have 2 files of below format. File1 AA~1~STEVE~3.1~4.1~5.1 AA~2~DANIEL~3.2~4.2~5.2 BB~3~STEVE~3.3~4.3~5.3 BB~4~TIM~3.4~4.4~5.4 File 2 AA~STEVE~AA STEVE WORKS at AUTO COMPANY AA~DANIEL~AA DANIEL IS A ELECTRICIAN BB~STEVE~BB STEVE IS A COOK I want to match 1st and 3rd... (2 Replies)
Discussion started by: crypto87
2 Replies

5. Shell Programming and Scripting

Text file to CSV with field data separated by blank lines

Hello, I have some data in a text file where fields are separated by blank lines. There are only 6 fields however some fields have several lines of data as I will explain. Also data in a particular field is not consistently the same size but does end on a blank line. The first field start with... (6 Replies)
Discussion started by: vestport
6 Replies

6. Shell Programming and Scripting

Input Validation of comma separated values

Hello all, I am working on a script and have the first part solved of numerical input validation. Below the code validates that the input is a numerical value between 100 and 1000. If not, it errors out. Now I need to be able to read values separated by a comma. For example, instead of my... (5 Replies)
Discussion started by: LinuxRacr
5 Replies

7. Shell Programming and Scripting

How to grep all lines from a file NOT having a certain character

Hi, I have for instance following INPUT file from which I want to grep ALL lines NOT containing the literal '{' into an OUTPUT file: ... RUNJOB=1,AxBxALLxGEx RUNJOB=0,AxBxDELxGExPRAEMxABLxZGS RUNJOB=0,AxBxDELxGExPRAEMxHARM RUNJOB=0,{UNIX: echo '§ASG§;%ASG_START}... (8 Replies)
Discussion started by: ABE2202
8 Replies

8. UNIX for Advanced & Expert Users

remove lines from file where > 13 occurrences of character

I have a '~' delimited file of 6 - 7 million rows. Each row should contain 13 columns delimited by 12 ~'s. Where there are 13 tildes, the row needs to be removed. Each row contains alphanumeric data and occasionally a ~ ends up in a descriptive field and therefore acts as a delimiter, resulting... (7 Replies)
Discussion started by: kpd
7 Replies

9. Shell Programming and Scripting

Replace a perticular character of all lines of a file

Hi all, I am new to UNIX, so sorry if my question seem stupid to u. well i want to replace the first character of first 30 lines of a file, only if the first character is h. and in anothe script i want to replace a particular string/character say hello/h of a file.Condition: It should... (1 Reply)
Discussion started by: abovais
1 Replies

10. Programming

character validation

how do i validate against charcaters in c ta (9 Replies)
Discussion started by: ruffenator
9 Replies
Login or Register to Ask a Question