LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 07-28-2010, 07:42 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
put string on top of the text


I want to create a script wherein it will put a string somewhere on the text file. But my priority was on top but I hope I can place it in any lines if possible. I tried to create a script using redirect ">" and then put it on top of the file.

The text contains:

Quote:
foo
fee
fii
fuu
I want to put "MY TEXT" so I did the script below:

Code:
#!/bin/bash

cat /root/tmp/text > /root/tmp/text.tmp
echo "MY TEXT" > /root/tmp/text
cat /root/tmp/text.tmp >> /root/tmp/text
rm -f /root/tmp/text.tmp
Is there other way I could do this?
 
Old 07-28-2010, 09:36 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Looks good to me

Minor variation:
Code:
echo "MY TEXT" > tmp
cat myfile >> tmp
mv tmp myfile
PS:
Always try to use "root" as SELDOM as possible (ideally, NEVER log on and NEVER do any serious activity as root).
 
Old 07-28-2010, 09:41 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
sed '1 i text' file #insert at line 1
sed '10 i text' file #insert at line 10
awk 'NR==1{print "text"}1 ' file  # insert at line 1
awk 'NR==10{print "text"}1 ' file  # insert at line 10
 
Old 07-28-2010, 09:42 PM   #4
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
@paulsm4

Quote:
echo "MY TEXT" > tmp
cat myfile >> tmp
mv tmp myfile
Thanks. Much lesser code much better

About the root thing. Thanks for the reminder :-)
 
Old 07-28-2010, 09:44 PM   #5
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
@ghostdog74

Thanks for this. Will try this one. I truly appreciate all your replies. I notice you're the only one who usually replies to my questions. Thanks for the LWQ Community.

Quote:
sed '1 i text' file #insert at line 1
sed '10 i text' file #insert at line 10
awk 'NR==1{print "text"}1 ' file # insert at line 1
awk 'NR==10{print "text"}1 ' file # insert at line 10
 
Old 07-28-2010, 10:33 PM   #6
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
Insert text somewhere in list file

Well ... this is not as elegant but I was bored and put this together:

Code:
#!/bin/bash

INSERT=0
INFILE=infile.txt
OUTFILE=outfile.txt
TEXT=""

function usage () {
cat << EOF

usage: $0 options

  -n   Number of lines into your file to enter text.  Default = 0 (top)
  -i   The file containing your text list. Default = infile.txt
  -t   [Mandatory] The text inside quotes that will be inserted at line 'n' inside file 'i'.
  -o   The file created by this script with the result of inserting 
       your test at line 'n' inside 'i' file. Default is outfile.txt

EOF
exit 0
}

function my_exit () {
	cat $OUTFILE
	exit
}

while getopts "hn:i:o:t:" flag
do
	case $flag in
	n)
		if [[ $OPTARG -gt 0 ]]
		then
			INSERT=$(expr $OPTARG - 1)
		else
			INSERT=$OPTARG
		fi
		;;
	i)
		if [[ -f $OPTARG ]]
		then
			INFILE=$OPTARG
		else
			echo "WARNING: infile does not exist.  Using infile.txt instead"
			echo -e "dummy \ndummy \ndummy" > infile.txt
		fi
		;;
	o)
		OUTFILE=$OPTARG
		if [[ -f $OPTARG ]]
		then
			echo "WARNING: outfile $OUTFILE exists.  Will overwrite."
		fi
		;;
	t)
		TEXT=$OPTARG
		;;
	h)
		usage
		;;
	\?)
		usage
		;;
	*)
		usage
		;;
	esac
done


if [ "x" == "x$TEXT" ]
then
	echo "-t  option is mandatory ... so enter some text in quotes"
	exit
fi


if [[ ! -f $INFILE ]]
then
	echo "WARNING: infile does not exist.  Using infile.txt instead"
	echo -e "dummy \ndummy \ndummy" > infile.txt
fi


TEMPFILE="blah.txt.$$"

if [[ $INSERT -eq 0 ]]
then
	echo "$TEXT" > $TEMPFILE
	cat $INFILE >> $TEMPFILE
	mv $TEMPFILE $OUTFILE
	my_exit
fi

TOTAL=$(cat $INFILE | wc -l)
HEAD=$INSERT

if [[ $HEAD -gt $TOTAL ]]
then
	cat $INFILE > $TEMPFILE
	echo "$TEXT" >> $TEMPFILE
	mv $TEMPFILE $OUTFILE
	my_exit
fi

TAIL=$(expr $TOTAL - $INSERT)

head -n $HEAD $INFILE > $TEMPFILE
echo "$TEXT" >> $TEMPFILE
tail -n $TAIL $INFILE >> $TEMPFILE
mv $TEMPFILE $OUTFILE
my_exit
Now run the program like this:

Code:
./test.sh -n 2 -t "hello there old man"
You should get this output:

Code:
tony@monarch:~$ ./test.sh -n 2 -t "hello there old man"
dummy 
hello there old man
dummy 
dummy
The text 'hello there old man' is placed at line #2 as specified by the -n switch in the output file ... and you will have infile.txt and outfile.txt on your system.

Last edited by tonyfreeman; 07-28-2010 at 10:38 PM. Reason: Explanation of code output.
 
Old 07-29-2010, 12:06 AM   #7
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
@tonyfreeman

Thanks for this sample script.
 
  


Reply



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
[SOLVED] C - How to put a specific arbitrary part of a string into it's own string? golmschenk Programming 9 04-19-2010 08:27 PM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
search string in text and print string wolfipa Linux - Software 4 09-17-2009 08:50 AM
Easy string/text manipulation/indentation for restructured text brianmcgee Linux - Software 1 04-22-2008 08:27 PM
How to put Xterm 'on top'? Infernox Linux - Newbie 4 12-26-2006 04:46 PM

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

All times are GMT -5. The time now is 12:16 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
Open Source Consulting | Domain Registration