Add new parameters into a line, and redirect the line to other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add new parameters into a line, and redirect the line to other file
# 1  
Old 04-07-2010
Add new parameters into a line, and redirect the line to other file

How can i add new parameters into a line, and redirect the line to other file?

For example:
1.sh
Code:
name:owner
google:richard
youtube:student

I want a, for example 2.sh with:
Code:
name:owner:description
google:richard:search site
youtube:student:video site

In the 2.sh, I added a new column: description and the parametrs.

I have builded a function to add things into file like:
addreg "$name:$owner"

Code:
addreg() {
    local key=$(echo "$1" | cut -d $SEP -f1)                     
    if existe "$key"; then
        echo "Error! The key '$key' is on the file"
        return 1
    else                                                             # new key
        echo "$*" >> "$c"                                 # save registry
        echo "Added '$key'!"
    fi
    return 0
}

I tried to make this but i cant get the result. The way I found to avoid this, is to put into the original file the fields like google:richard:description and when I want to insert the description I search the words description and edit to the $description that I insert.

Hope you can understand my englishSmilie

---------- Post updated at 05:40 PM ---------- Previous update was at 03:15 PM ----------

Can anyone help me?

Last edited by rafazz; 04-07-2010 at 06:43 PM..
# 2  
Old 04-07-2010
I'm sure there's much more to your process than you've listed; for example, I don't see where you're getting the third column's contents...and your function's $c.

However, please note:

1) Probably a Typo in your "if" statement (existe should be exists...?);

2) Assuming you're getting your third column contents from somewhere:

Code:
$ tail -3 file_test.txt |while read LINE ;do print "$LINE:$LINENO" ;done
name:owner:1
google:richard:1
youtube:student:1

# 3  
Old 04-07-2010
the $c is where the data is, like $c=file.txt.

The third column is asked to the user, with a echo "add description" read description.

I dont know how I can add a third column in the line. I think, this might result. I will test and thank you!

---------- Post updated at 09:24 PM ---------- Previous update was at 09:19 PM ----------

The tail function is perfect i think. But the number -3 is the number of lines that will be extracted. How can i check the number when i dont know how many lines are in the file? Use a counter right? and if I wanna extract a certain line like the line with $name google. How can i do that?

---------- Post updated at 09:25 PM ---------- Previous update was at 09:24 PM ----------

Tail is a great anyway!

tail MAN Page
# 4  
Old 04-07-2010
tail was just an example to dump the file. You can use anything; head, tail, more, grep...
# 5  
Old 04-08-2010
if I wanna extract a certain line like the line with $name google. How can i do that?
# 6  
Old 04-08-2010
Quote:
Originally Posted by rafazz
if I wanna extract a certain line like the line with $name google. How can i do that?
grep is your friend in that case.

Regards
# 7  
Old 04-08-2010
program.sh
Code:
source functions.sh
...

if exists "$name";then
	move "$name"
else
	echo "Error: name not here in $c"
fi

...

functions.sh
Code:
$c=1.txt
$v=2.txt

# it checks if $1 is in the file
exists() {
	grep -i -q "^$1$SEP" "$c"
}

#insert info in 2.txt

insert_in_2() {
	local chave=$(echo "$1" | cut -d $SEP -f1)                         

	if exists "$key"; then
		echo "The key '$key' is reg in <1.sh>."
		return 1
	else                                         	                      # new key
		echo "$*" >> "$v"                     			      
		echo "Reg '$chave' added with sucess in <2.txt>."
	fi
	return 0
}

#delete the line in 1.txt and moves the line to 2.txt
move(){
	exists $1 || return
	echo -n "Insert description "
	read description
	
	aux="$(grep -i -v "$name" "1.txt")"  
	insert_in_2 "$aux:$description"
	echo
	echo "The description in '$1' added with sucess!"
		}

This is my code, but it copy the all lines in 1.txt but I only want the line that $name is in! How can i do that?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. 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

5. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

6. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Shell Programming and Scripting

howto add line as a first line into a non empty file

Hi Trying to do like this : echo "$variable1\n $(cat file.txt)" but it only adds one time. When I run this cmd again with different variable it only replaces line of variable1. How to add constantly line into first line in file ? (3 Replies)
Discussion started by: presul
3 Replies

8. Shell Programming and Scripting

Want to use sed to add some parameters at the end of the line

Hello All, I have a doubt in sed, i want to add some parameter at the end of the tag inside a xml tag. how to i do that. so i want to add Results="true" value="high" inside the xml tag. Orignal <execute description="reboot"> <execute description="Stop Servlet"> After adding the... (5 Replies)
Discussion started by: asirohi
5 Replies

9. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies
Login or Register to Ask a Question