LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2005, 11:01 AM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,286

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
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,
 
Old 09-21-2005, 11:33 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
sed -i (RTM)
 
Old 09-21-2005, 12:28 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,286

Original Poster
Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
huh:
Code:
sed -i (RTM)
ksh: 0403-057 Syntax error: `(' is not expected.
 
Old 09-21-2005, 12:42 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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.
 
Old 09-21-2005, 01:33 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,286

Original Poster
Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
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,
 
Old 09-21-2005, 01:41 PM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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)
 
Old 09-21-2005, 01:59 PM   #7
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
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.
 
Old 09-21-2005, 02:00 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,286

Original Poster
Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
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 ?

Last edited by schneidz; 09-21-2005 at 02:06 PM.
 
Old 09-21-2005, 02:56 PM   #9
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Copy and Overwrite jrdioko Linux - Newbie 2 06-29-2004 11:46 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Can't overwrite MBR Elmo Linux - General 2 03-13-2003 01:24 PM
overwrite a harddisk macone Linux - General 1 12-17-2002 08:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration