![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 invalid_file.txt should contain: Code:
How|Are|You Hello Code:
Hi|Hello Thanks, SK Last edited by Yogesh Sawant; 05-27-2008 at 07:08 AM.. Reason: added code tags |
|
||||
|
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 |
|
||||
|
You could have used awk:
Code:
awk -F "|" 'NF!=2' file > output |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|