How can i add new parameters into a line, and redirect the line to other file?
For example:
1.sh
name:owner
google:richard
youtube:student
I want a, for example 2.sh with:
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"
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 english;)
---------- Post updated at 05:40 PM ---------- Previous update was at 03:15 PM ----------
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:
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 ----------
source functions.sh
...
if exists "$name";then
move "$name"
else
echo "Error: name not here in $c"
fi
...
functions.sh
$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?