LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed overwrite (https://www.linuxquestions.org/questions/programming-9/sed-overwrite-365620/)

schneidz 09-21-2005 11:01 AM

sed overwrite
 
hi lq,

i have this
Code:

sed -p s/^$/0/g lq.txt
how do i overwrite the file lq.txt instead of printing to the screen.

thanks,

jonaskoelker 09-21-2005 11:33 AM

sed -i (RTM)

schneidz 09-21-2005 12:28 PM

huh:
Code:

sed -i (RTM)
ksh: 0403-057 Syntax error: `(' is not expected.


druuna 09-21-2005 12:42 PM

Hi,

-i only works if sed 4.X or better is used, prior to that it takes a bit more then just one parameter:

sed -p s/^$/0/g lq.txt > /tmp/lq.txt
mv /tmp/lq.txt .


Hope this helps.

schneidz 09-21-2005 01:33 PM

sed: filter dupes
 
yeah that's the patch i used is my script. thanks guys,

amways, how do i use sed to leave the first of a pattern untouched. then print the line number where the same pattern exists...

thanks,

jonaskoelker 09-21-2005 01:41 PM

sed can do what you're asking for (it's turing-complete), but you might want to look into something else--awk springs to mind.

I'd probably do it in python:

Code:

from sys import stdin
lines = stdin.readlines()
visited = set()
for i in xrange(len(lines)):
    line = lines[i]
    if line in visited: print i, line
    visited.add(line)


sirclif 09-21-2005 01:59 PM

perhaps grep does what your looking for?

> grep -n pattern filename

this prints the line number and line that the matches pattern. if you only want the line number, you could pipe it to something that will filter out the line and leave the line number:

> grep -n pattern filename | cut -d: -f1

the lines, with thier line number, are piped into cut, which sets the field delimiter to ':' (-d:) and cuts out all but the first field (-f1), then prints the result to the screen.

schneidz 09-21-2005 02:00 PM

i dont think aix has python:

/> which python
which: 0652-141 There is no python in $path

awk might work. what i want is if col5 is uniq then add col 11 to total.

i would like to keep the non-uniq rows but change col 11 to 0 for the repeated col 5's.

any help is appreciated...

is there a way to tell awk what lines to operate on ?

sirclif 09-21-2005 02:56 PM

yes, you can tell awk to only work on certain lines. do a google for awk/gawk reference and look for 'addresses'

for example:

> gawk '/pattern/ {print $2}'

says to print the second field of all lines (or records) that contain 'pattern'

> gawk 'NR > 5 {print $1}'

prints records (lines) 6 and up.


All times are GMT -5. The time now is 04:28 PM.