LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2008, 09:02 PM   #1
lumix
Member
 
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323

Rep: Reputation: 30
Is it possible to have sed only process starting from, say, the 9th character on?


If I want to replace characters, but only starting after a certain position on the first line, how can I do this?
 
Old 05-13-2008, 09:31 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
OOPs--it did not work!!

First, I tried using a backreference in SED--I saved the first 8 characters + any number additional before the phrase to be matched. The problem occurs when there are two instances on the same line. The regex is "greedy" and goes all the way to the last instance.

Thinking......let's see who solves this before I do..

Last edited by pixellany; 05-13-2008 at 09:53 PM. Reason: boo-boo
 
Old 05-13-2008, 10:21 PM   #3
rlhartmann
Member
 
Registered: Mar 2008
Posts: 73

Rep: Reputation: 16
Good guestion, you could do something using the regular expression wild card "." 8 times, but I don't think that's what your looking for. You can do it in steps using cut, sed, and paste, here is an
example script [B]./sedex.bash[B]:

Quote:
#!/bin/bash

FILE="$1"

cut -c1-8 $FILE >$FILE.a
cut -c9- $FILE | sed "s/Hello/Goodbye/" >$FILE.b

paste -d"|" $FILE.a $FILE.b | sed "s/|//" >$FILE.out

cat $FILE.out
Given a datafile called test.dat like:
Quote:
abcdefghHello world
abcdefghHello Class
Run the script using ./sedex.bash test.dat the output
is:
Quote:
abcdefghGoodbye world
abcdefghGoodbye Class
 
Old 05-13-2008, 10:26 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
sed -n 'h;s/\(.\{8\}\).*/\1/;x;s/\(.\{8\}\)//;s/OLD/NEW/g;H;g;s/\n//;p' filename
Thereby demonstrating that SED can rival C in having inscrutable code....

What it does:
copy the whole line to the hold buffer
strip all but the first 8 chars and swap with contents of hold buffer
substitue "NEW" for "OLD" and append to the hold buffer
recall contents from hold
remove the newline (\n) and print.

I have total confidence that someone will post a better solution.......
 
  


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
HP-UX newline character with sed jhwilliams Other *NIX 8 08-06-2007 06:13 PM
Insert after every nth character using sed DoDo1975 Linux - Software 2 01-17-2007 07:59 PM
What is the ninth (9th) character or field in the output string of rpm -V ? zimon Linux - Distributions 1 08-28-2006 03:47 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 06:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration