Add Some String on each line of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add Some String on each line of file
# 1  
Old 02-23-2012
Add Some String on each line of file

HI Guys,

I am new user its my first post.

I have one file in DIR XYZ.

File name is ABCDEF.log

Now i want to add that file in each line of file.

I have data in file ABCDEF.log
Code:
TestKLFH
TEstLKHU
HESTLJHG

Now i want to update the file with below data
Code:
ABCDEFG.log:TestKLFH
ABCDEFG.log:TEstLKHU
ABCDEFG.log:HESTLJHG

One more Thing ,I want do this if DIR XYZ have only one file.otherwise just exit.

Thanks In Advance.

Last edited by Franklin52; 02-24-2012 at 03:33 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-23-2012
Code:
cd XYZ
for i in *.log
do
        if [ -f "$i" ]; then
                sed -i "s/^/$i:/" "$i"
        fi
done

# 3  
Old 02-23-2012
Hi I am getting error.

sed: illegal option -- i
# 4  
Old 02-24-2012
Quote:
Originally Posted by asavaliya
Hi I am getting error.
sed: illegal option -- i
As the error says your Sed does not support -i option. Try if the below helps..
Code:
DIR="/home/abc/xyz"

file_count=$(ls $DIR |wc -l) # Get the count of files present in the DIR

if [[ $file_count -eq 1 ]] # if count equals 1 then,
then
	
	file_name=$(ls $DIR) # store the file name
	sed "s/^/$file_name: /" ${DIR}/${file_name} > ${DIR}/output.txt # append the required TEXT and store it in output.txt 

	if [[ $? -eq 0 ]] # if the above command is success then move
	then
		mv ${DIR}/output.txt ${DIR}/${file_name}
	else
		echo "Command failed to update file"
	fi
fi

This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 02-24-2012
Thanks a lot its working fin.
# 6  
Old 02-25-2012
Code:
$ [ $(ls -1 /dir/path/XYZ | wc -l) -eq 1 ] && awk '{print FILENAME":"$0}' ABCDEF.log > outfile && mv outfile ABCDEF.log

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

2. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

6. Shell Programming and Scripting

add string and time stamp on each line of file

I have file A.txt A 1023 B 123 C 1223 I want output Hello_12PM_A 1023 Hello_12PM_B 123 Helll_12PM_C 1223 Add Hello and time stamp in AM and PM. (4 Replies)
Discussion started by: asavaliya
4 Replies

7. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

8. Shell Programming and Scripting

Search a string and to add another string after that in new line

Hi Guys I am facing a problem:wall: In searching a string in a file and to add another string(ie. passed through command line argument) just after this(searched) string in new line. Thanks (2 Replies)
Discussion started by: kushwaha
2 Replies

9. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies
Login or Register to Ask a Question