The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
"|" separated file validations kolesunil Shell Programming and Scripting 1 05-27-2008 05:19 AM
Separate lines in a single '|' separated line hidnana Shell Programming and Scripting 3 03-17-2008 10:16 AM
Replace a perticular character of all lines of a file abovais Shell Programming and Scripting 1 12-11-2007 05:35 AM
two lines into one colon separated line... tonlu Shell Programming and Scripting 2 03-30-2005 10:27 AM
character validation ruffenator High Level Programming 9 05-02-2002 02:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2008
kolesunil kolesunil is offline
Registered User
  
 

Join Date: May 2008
Posts: 8
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 07:08 AM.. Reason: added code tags
  #2 (permalink)  
Old 05-27-2008
kolesunil kolesunil is offline
Registered User
  
 

Join Date: May 2008
Posts: 8
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 07:09 AM.. Reason: added code tags
  #3 (permalink)  
Old 05-27-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
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 (permalink)  
Old 05-27-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
You could have used awk:
Code:
awk -F "|" 'NF!=2' file > output
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0