Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unix sed

Posted on 2003-03-04
11
Medium Priority
?
527 Views
Last Modified: 2012-06-22
Hello, everyone, I'm new to this board, and I hope I'm posting to the right area.

I'm working with sed, and I'm wondering if there's a way to copy a specific part of a text file to another part of the file.
For example, if I have a one-line text file like this...

This is a test

Can I use sed somehow to make it look like this...

This is a test     This is a test

I'm hoping there's a way to specify exact columns to tell sed to copy columns 1-10 to columns 30-40.

Any help would be greatly appreciated!
0
Comment
Question by:Eric_1969
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 8

Expert Comment

by:heskyttberg
ID: 8067315
Hi!

I don't think sed is what you are looking for here.
Sed is a text editor so to speak nothing else.

You should look into awk or gawk.

With this you could make such scripts to copy columns cause awk is almost a text based database handler.

Regards
/Hans - Erik Skyttberg
0
 
LVL 2

Accepted Solution

by:
jimbb earned 200 total points
ID: 8068686
I'm going to make an assumption here (since your example text is longer than 10 chars) that what you really want is to divide the screen width into two 30-char-wide fields, and output your string left-justified into each field (the second field starting in column 31)?

$ echo "This is a test" | awk '{printf("%-30s%-30s\n", $0, $0)}'
This is a test                This is a test

If that's not exactly the output you want, you probably can tweak those format strings to get what you want.  man awk and look at the 'printf' command; also there's some good documentation in the printf(1) and printf(3) man pages.

Feel free to follow-up to this if you get stuck!
0
 
LVL 4

Expert Comment

by:Vinit Kain
ID: 8070898
Try This,

excute this script with 4 parameters in your case put 1 10 30 40


#!/bin/ksh
cat testfile |awk -v s1=$1 -v e1=$2 -v s2=$3 -v e2=$4 '{
if((NF<e1)||(s2<e2)||((e1-s1)!=(e2-s2))||(NF<s2))
{
        add1=s2-NF
        printf $0;
        for(i=1;i<=add1;i++)
        printf "        ";
        for(i=s1;i<=e1;i++)
        printf $(i)" ";
}
else
{
        print "Error"
}
}'
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Expert Comment

by:Gns
ID: 8079369
The silly sed thing to do is something like:
sed -e 's/\(.\{10\}\)\(.\{20\}\)\(.\{0,\}\)/\1\2\1\3/' file
where the \{0,\} is a sligtly more portable \* or even \+.
Lines that simply don't match (ie have 30+ character columns) will be emitted "as-is".

-- Glenn
0
 
LVL 20

Expert Comment

by:Gns
ID: 8079389
grrr. "(ie have 30+ character columns)" should read "(ie don't have 30+ character columns)"

-- Glenn (a.k.a. Le Grand Typo)
0
 

Expert Comment

by:sandtrap
ID: 8090069
sed 's/this is a test/&  &/g' textfile

will return:
this is a test this is a test

so sed will search of this is a test and the "&" signs will echo whatever it matches on the replace side


hope this helps
0
 
LVL 20

Expert Comment

by:Gns
ID: 8093075
sandtrap, do try my example. It will juggle three "matched areas" to more closely mimik the expressed need of copying the first 10 characters to position 30-39, and still retain any data from position 30 to EOL "pushed right" to position 40.

The grouping is done with the \( \) construct, where the first group match \1, the second \2 etc. The actual matching is done via the c\{m,n\} construct, where c == character to match ("." in this case, which match any character), m == least amount of matches and n = upper limit.

But as stated (or rather implied) in my earlier comment, sed is silly here since it will only operate correctly under a limited set of assumptions, and it is very difficult to make it more generic than my suggestion. Many have already suggested awk/gawk which is fine (apart from the limitations:-). Normally I'd do this kind of thing with perl.

-- Glenn
PS for Hans-Erik Skyttberg. Are you swedish? Ever go to the university of Luleå?
DS
0
 
LVL 8

Expert Comment

by:heskyttberg
ID: 8093582
Hi!

Yes, I'm swedish no I never went to Lulee, only Umea.

Regards
/Hans - Erik Skyttberg
0
 
LVL 20

Expert Comment

by:Gns
ID: 8101387
Ok, I wen't to Lulea ... some time ago:-) (I can't believe EE isn't 8-bit clean... I typed "a ring":-). Used to be a very intelligent person by name of .. Hans-Erik S...berg, who I've completely lost contact with. Turns out it was Sandberg, not Skyttberg.

Fun to see a countryman though;-)

-- Glenn
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question