LinuxQuestions.org
Register a domain and help support LQ
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 11-21-2012, 01:48 AM   #1
azheruddin
Member
 
Registered: Dec 2011
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
inserting text in a file at particular position


HI

I have a situation like need to insert some wrods at the the last line...but
1st word starts at 1st position and ends...
2nd word start from 25th position and ends..
3rd word starts from 51th postition

these words are coming through variables so i think sed utility will work perfectly here but how ..

e.g

$apple $banana $watermalon


can you please help out here.......

Regards,
Ajju
 
Old 11-21-2012, 04:25 AM   #2
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,441

Rep: Reputation: 81
Here's an example of how to insert a hyphen after the first fourth character.

echo "201107" | sed 's/^\(.\{4\}\)/\1-/'

Without escaping the metacharacters, it's much easier to read and understand this regular expression.

^(.{4})

^ = start of line
. = match any single character
{4} = quantifier, how many times to match the previous expression
.{4} means match any single character 4 times
() grouping. this is used so that ^ doesn't apply to just the .
It should be applied to the entire expression in the group.
This is similar to order of operation in math.

Next:
\1 backreferencing, first match
insert a - after the matched pattern

Last edited by fakie_flip; 11-21-2012 at 04:34 AM.
 
1 members found this post helpful.
Old 11-21-2012, 04:28 AM   #3
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,441

Rep: Reputation: 81
This could also be done using a loop iterating through each character, then echo a hypen and continue iterating through the word. If you need to keep this value instead of just print it, then you would need to create a new variable to copy the above steps to.
 
Old 11-21-2012, 05:01 AM   #4
azheruddin
Member
 
Registered: Dec 2011
Posts: 56
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
SED or AWK

MY requirement is that

we have on file file1 with data like

apple banana watermelon
Apricot BlackBerry Grape
Guava Jack fruit Lime
strawberry pineapple plum

so here I need to add one more row like
pommel orange Fig

so above line should be appended to file1 [at last line]

sed or awk it is not constraint ,only it should work out.
 
Old 11-21-2012, 05:28 AM   #5
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,441

Rep: Reputation: 81
echo 'pommel orange Fig' >> file1

Or if you're using a variable, use double quotation marks:

echo "$fruit_salad" >> file1

>> is a bash redirection operator

You can google "bash redirection" to find out about all the different bash redirection operators.

>> means append the output to a file

Last edited by fakie_flip; 11-21-2012 at 05:30 AM.
 
Old 11-21-2012, 10:52 AM   #6
azheruddin
Member
 
Registered: Dec 2011
Posts: 56
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
SED or AWK

Sorry to say but point is here that....

above in a given example..

echo 'pommel orange Fig' >> file1

Pommel is ok ---it is starting from 1st position but problem with orange and Fig...

orange should start from 25th position in a line including spaces
Fig should start from 51th position in a line including spaces..

Why iam telling this we dont know the size of variable value..it nay be 4 or it may 14...the next varibale should insert at 25th postion onwards.....


output should be like...

pommel orange fig
123456.................25..................51...

Hope you are getting my point..
 
Old 11-21-2012, 12:22 PM   #7
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: /root
Distribution: Slackware & BSD
Posts: 1,223

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by fakie_flip View Post
Here's an example of how to insert a hyphen after the first fourth character.

echo "201107" | sed 's/^\(.\{4\}\)/\1-/'

Without escaping the metacharacters, it's much easier to read and understand this regular expression.

^(.{4})

^ = start of line
. = match any single character
{4} = quantifier, how many times to match the previous expression
.{4} means match any single character 4 times
() grouping. this is used so that ^ doesn't apply to just the .
It should be applied to the entire expression in the group.
This is similar to order of operation in math.

Next:
\1 backreferencing, first match
insert a - after the matched pattern
Thanks fakie...
 
Old 11-30-2012, 01:58 AM   #8
azheruddin
Member
 
Registered: Dec 2011
Posts: 56
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
SED or AWK

I still didnt get any response on...iam again repeating my question with example..

i have one file say infile like

America China Japan
Australia Newzland italy
Jamaica jermany holand

so my point is that i want to insert a new a row at last in same format in infile.
say..
India Holand England

these three countries are coming through variables...

using sed or AWK i doesnt matter....
 
Old 11-30-2012, 02:08 AM   #9
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
I don't understand from where comes the decision [i]where[/] (on which position) the words have to be inserted.

Could you please explain 1. Which words 2. Where and 3. why there?

Markus
 
Old 11-30-2012, 04:38 AM   #10
azheruddin
Member
 
Registered: Dec 2011
Posts: 56
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
ok Mark find your answers

1.so there are three variables coming through command line arguemnts to the script, so through script iam trying to update a file.

2. in anyfile say file1 ,i have mentioned this in script

3.why there means i need a file in a same format that is columnar format ,three columns.
 
Old 11-30-2012, 07:51 AM   #11
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,520

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
If I understand correctly, your original file, say file.txt, looks like
Code:
123456789_123456789_123456789_123456789_123456789_123456789_
apple                   banana                    watermelon
Apricot                 BlackBerry                Grape
Guava                   Jack fruit                Lime
strawberry              pineapple                 plum
Perhaps a bash script, say test.sh, like this will work
Code:
#!/bin/bash

printf "%-24s%-25s%-24s\n" $1 $2 $3;
So './test.sh India Holland England >> file.txt' produces
Code:
123456789_123456789_123456789_123456789_123456789_123456789_
apple                   banana                    watermelon
Apricot                 BlackBerry                Grape
Guava                   Jack fruit                Lime
strawberry              pineapple                 plum
India                   Holland                   England

Last edited by allend; 12-01-2012 at 12:26 AM. Reason: Script revised to a more compact printf statement
 
Old 12-03-2012, 01:14 AM   #12
azheruddin
Member
 
Registered: Dec 2011
Posts: 56
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Smile

Thank You very much all its worked out!
 
  


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
Inserting a block of text into a text file on system boot krptodr Linux - Newbie 5 02-14-2012 08:11 PM
Script for parsing a text file and inserting into DB towhidgrep Programming 4 06-20-2011 09:20 AM
inserting some text into a file using 'awk' command eliote Programming 16 10-26-2010 04:16 AM
inserting/deleting characters into a text file ananthbv Programming 7 07-14-2004 12:40 AM
inserting text into a file DavidPhillips Programming 5 08-15-2003 05:53 PM


All times are GMT -5. The time now is 03:04 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration