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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can I read a file character by character? murtaza Shell Programming and Scripting 4 04-27-2009 05:51 AM
Using input file to filter data from another file tumblez UNIX for Dummies Questions & Answers 1 07-21-2008 04:21 PM
check for a particular character inside a file and substitute with a given character? karthikprasathk AIX 1 07-01-2008 03:29 AM
Can i read a file character by character karnan Shell Programming and Scripting 6 05-19-2008 02:22 AM
how to get input from file ajaya Shell Programming and Scripting 1 04-05-2006 02:02 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 08-07-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
How can I get rid of the ` character from input file?

A sed command works most of the time however it fails sometimes.

I put each line (record) I read of a file through the following command
data=$(cat file | sed 's/[^a-zA-Z0-9+_:-]//g' | sed 's|.*ex:Msg\(.*\)ex:Msg.*|\1|' )

When I run the script I get a message that states that there is an invalid format character.

The list of invalid chracters I am getting is as below, is there a way to solve this issue?. How can I get rid of the ` character from the input data?

`w'
`m'
`k'
``'
`;'
`O'
`m'
`H'
`^'
` '
`:'
`v'
`S'
`k'
`j'
`)'
`!'
`['
`m'
`@'
`T'
  #2 (permalink)  
Old 08-07-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool Does the following work?

Since the character is the first in each line...

Code:
cat file | cut -c2-
  #3 (permalink)  
Old 08-07-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
Its not the first character

I am afraid its not the first character, I have specified the message that is displayed on the screen when I run my script which is of the format ./records.sh invalid chracters....etc
  #4 (permalink)  
Old 08-07-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Post input data

Can you provide a sample of the input file
  #5 (permalink)  
Old 08-07-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

To answer the question in the subject line:

Code:
tr -d '`' < FILE
Quote:
Originally Posted by gugs View Post
A sed command works most of the time however it fails sometimes.

Under what conditions does it fail?
Quote:
I put each line (record) I read of a file through the following command

I hope you don't really run the entire file through two instances of sed (not to mention cat) for every line in the file.
Quote:
data=$(cat file | sed 's/[^a-zA-Z0-9+_:-]//g' | sed 's|.*ex:Msg\(.*\)ex:Msg.*|\1|' )

You are using three external commands where you only need one:
Code:
data=$( sed -e 's/[^a-zA-Z0-9+_:-]//g' -e 's|.*ex:Msg\(.*\)ex:Msg.*|\1|' file )
Quote:
When I run the script I get a message that states that there is an invalid format character.

Please post the exact message you get. (Cut and paste it, don't retype it.)
Quote:
The list of invalid chracters I am getting is as below, is there a way to solve this issue?. How can I get rid of the ` character from the input data?

Why do you think the problem is the ` character?

The invalid characters are those between the quotes.
Quote:

`w'
`m'
[snip]
`@'
`T'
  #6 (permalink)  
Old 08-08-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
Sample data

cfajohnson, unfortunately I cannot post a copy of the data because I am not allowed, confidential info. The only reason I know that these chars are causing a problem is because I get an on line message: ./counter.sh line 5: printf: `w: invalid format and this is repeated for characters that I have mentioned in my initial posting. The contents of counter.sh is:

counter=1
while read line
do
# Test the file
printf "$line" > temp$counter
pref=$(sed <$temp$counter -e 's/[^a-zA-Z0-9+_:-]//g' -e 's|.*ex:Msg\(.*\)ex:Msg.*|\1|')
printf"
let counter=counter+1
done < temp01

Also you mention that I could use only one command instead of three for extarcting the data. Could you please specify? I am new to scripting so any help would be greatly appreciated.

Code:
data=$( sed -e 's/[^a-zA-Z0-9+_:-]//g' -e 's|.*ex:Msg\(.*\)ex:Msg.*|\1|' file )
  #7 (permalink)  
Old 08-08-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by gugs View Post
cfajohnson, unfortunately I cannot post a copy of the data because I am not allowed, confidential info. The only reason I know that these chars are causing a problem is because I get an on line message: ./counter.sh line 5: printf: `w: invalid format

As the message says, you are using an invalid format character with printf.

The syntaxt for printf is:
Code:
printf FORMAT_STRING ARG ...
What you need is:
Code:
printf "%s\n" "$line"
Quote:
and this is repeated for characters that I have mentioned in my initial posting. The contents of counter.sh is:

Please put code inside code tags.
Quote:
Code:
 
counter=1
while read line
do
  # Test the file 
  printf "$line" > temp$counter
  pref=$(sed <$temp$counter -e 's/[^a-zA-Z0-9+_:-]//g' -e 's|.*ex:Msg\(.*\)ex:Msg.*|\1|')
printf"

Don't use sed for every line in a file; it will be extremely slow.

Use sed on the entire file and work on its output.
Quote:
let counter=counter+1

Use the standard form for arithmetic:
Code:
counter=$(( $counter + 1 ))
Quote:
Code:
 
done < temp01
Also you mention that I could use only one command instead of three for extarcting the data. Could you please specify? I am new to scripting so any help would be greatly appreciated.

Code:
data=$( sed -e 's/[^a-zA-Z0-9+_:-]//g' -e 's|.*ex:Msg\(.*\)ex:Msg.*|\1|' file )
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 11:48 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