Home using sed with variables in CSH
Reply: 0

using sed with variables in CSH

user1175 Published in June 20, 2018, 10:40 pm

hopefully this makes sense. My ideal goal is to read a file and change a variable with another variable, but the new variable is incremented each time.


input file -

SAK, 123, 345
SAK, 456, 789
SAK, 789, 901

What I want my output to look like -

1, 123, 345
2, 456, 789
3, 789, 901

As you can see SAK is being replaced with a number, but the number is incremented every time.

I tried doing this:

#!/bin/csh -f
set filename=/export/home/develop/data/buy_load_file.dat

set line=1
set sak_num=1
set search=SAK

set totallines=`wc -l $filename | cut -f1 -d' '`

echo $totallines

while( $line <= $totallines )
`sed "$line s/$search/$sak_num/" $filename`
@ line++
@ sak_num++

But the sed command never works for me. I tried reading different ways to do it, but I think my issue is with $filename that it's not able to read it properly. I think awk could be another command that could possibly do what I want it to do, but not sure how.

Any ideas? Or maybe an easier way for me to get the output I desire. Thanks!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.327433 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO