1830892 Members
2524 Online
110017 Solutions
New Discussion

perl, sed or awk

 
SOLVED
Go to solution
Pando
Regular Advisor

perl, sed or awk

I have a file which contain the line
...
...
"-"
...
...
I need to get the "-" part to be replaced by a variable content using perl or sed or awk.

Maximum points for correct reply!
11 REPLIES 11
Joseph Loo
Honored Contributor
Solution

Re: perl, sed or awk

hi,

try this:

# sed 's/"-"//' >

e.g.
# sed 's/"-"/abc/' to_be_replace > replaced

regards.
what you do not see does not mean you should not believe
Indira Aramandla
Honored Contributor

Re: perl, sed or awk

Hi,

using sed
substitute (find & replace) "-" with "$" on each line

sed 's/â -â /â $â /' # replaces only 1st instance in a line

sed 's/â -â /â $â /4' # replaces only 4th instance in a line

sed 's/â -â /â $â /g' #
Never give up, Keep Trying
Indira Aramandla
Honored Contributor

Re: perl, sed or awk

Hi,
Sorry about the font.

using sed
substitute (find & replace) "-" with "$" on each line

sed 's/"-"/"$"/' # replaces only 1st instance in a line

sed 's/"-"/"$"/4' # replaces only 4th instance in a line

sed 's/"-"/"$"/g' #
Never give up, Keep Trying
Biswajit Tripathy
Honored Contributor

Re: perl, sed or awk

cat in_file | awk -F- '{printf ("%s $variable_content %s\n", $1, $2)}'

As I'm not logged into a hp-ux system right now,
I can't test if '-' char as field separator needs to be
escaped or not (i.e -F- or -F\-).

- Biswajit
:-)

Re: perl, sed or awk

Probably a hundred different answers in perl, sed or awk.

My attempt in awk...

$cat awkit
awk '
{
if ($1 == "\"-\"")
printf("%s\n", "'$2'")
else
print $0
}' $1

if your file called foo then ...

$awkit foo my_variable

however I must admit that if your variable has spaces in then it doesn't work! i.e.

$awkit foo "my variable"

bombs out I guess you could add $3 to the script to fix - but there must be a better way

Hello World
Tim D Fulford
Honored Contributor

Re: perl, sed or awk

perl -i.org -ane 'if (m/[0-9]+\"\-\"[0-9]/) { ($id1,$id2)=split /\"-"/; print "$id1 somethinghere $id2";} else { print "@F\n";}

this will do an in-place edit of the file & save the original to *.org

You obviously need to say what somethinghere is.

Assume you want to replace "somethinghere" with something like mod 4 subid1 them

perl -i.org -ane 'if (m/[0-9]+\"\-\"[0-9]/) { ($id1,$id2)=split /\"-"/; $somethinghere=$id1%4; print "$id1 $somethinghere $id2";} else { print "@F\n";}'

Regards

Tim
-
Anupam Anshu_1
Valued Contributor

Re: perl, sed or awk

Hi Fernando,

I think I found a way to use sed for this purpose.

I created a file by name test1.

$ cat test1
"-"

wrote a small script:
---------------------------------------

rep=AAAA #Let the replacement be AAAA
rep_string="s/-/$rep/g" # Replace string built to be used by sed(1).

sed $rep_string test1 # Pass rep_string to sed(1).

Output (on Linux and HP-UX):
"AAAA"

------------------------

Hope this helps and I get maximum point ;-)

Cheers,

Anupam Anshu
Stuart Browne
Honored Contributor

Re: perl, sed or awk

Or perl one-liner with shell environment variable (REPLACE_TEXT) containing what you want to replace it with:

perl -pi.org -e 's/()"-"(<\/SUBID>)/\1$ENV{REPLACE_TEXT}\2/' content_file

No need to get overly complicated *shrug*.
One long-haired git at your service...
Bob_Vance
Esteemed Contributor

Re: perl, sed or awk

------------------------------------------
For *exactly* a "dash" ("-") as you stated) use:
------------------------------------------

# f=/tmp/file
# cat $f # already has data in it
aa

"-"
cc
dd

# tf=/tmp/ff
# newtext='hohoho'
# sed < $f > $tf \
-e '/"-"<\/SUBID>/s/"-"/"'${newtext}'"/'
# cp $tf $f
# cat $f
aa

"hohoho"
cc
dd


------------------------------------------
For *anything* between the original double quotes:
------------------------------------------

# tf=/tmp/ff
# f=/tmp/file
# newtext='hehehe'
# sed < $f > $tf \
-e '/".*"<\/SUBID>/s/".*"/"'${newtext}'"/'
# cp $tf $f
# cat $f
aa

"hehehe"
cc
dd


HTH
bv
"The lyf so short, the craft so long to lerne." - Chaucer
Nguyen Anh Tien
Honored Contributor

Re: perl, sed or awk

This is my answer (like Biswajit Tripathy solution)
cat test.pass|awk -F- '{printf("%s Variable %s\n",$1,$2)'}
NOTE:$1,$2 is column order in your file
HTH
tienna
HP is simple
Pando
Regular Advisor

Re: perl, sed or awk

Hi All,

Many thanks for your replies. It was a great help!