name:age:girlfriend
aa:11:yes
bb:22:yes
cc:33:no
dd:44:no
ee:55:yes
I want to change the cc, the status of girlfriend to yes.
Find cc, and then change! how can i do this?
name:age:girlfriend
aa:11:yes
bb:22:yes
cc:33:no
dd:44:no
ee:55:yes
I want to change the cc, the status of girlfriend to yes.
Find cc, and then change! how can i do this?
awk -F ':' { if($1=="cc" && $3=="no") { $3="yes"}; print $0}' inputfile > outputfile
Try this : I hope below is your expectation:
awk -F ':' '{if ($1 == "cc") {print $1":"$2":yes"} else {print $1":"$2":no"}}' inputfile
awk -F: '$1=="cc" {$3=($3=="yes")?"no":"yes"}1' OFS=: myFile
It dont work. I want to find the key, change the key in the file. The awk finds the key but dont change
(rm myFile; awk -F: '$1=="cc" {$3=($3=="yes")?"no":"yes"}1' OFS=: > myFile;) < myFile
$c is the file
$SEP is the separator - :
I have the function exists to find the key.
exists() {
grep -i -q "^$1$SEP" "$c"
}
So the prog will be
if exists "$name"; then
<CODE HERE>
echo
echo "Status changed"
else
echo "Error: status not found"
fi
The tr works but it change all the fields with the Yes to No, or No to Yes. but i only want that $name.
jim mcnamara idea seems to work if we add a quote and put the field separator back to ":".
awk -F ':' '{ if($1=="cc" && $3=="no") { $3="yes"}; OFS=":"; print $0}' inputfile > outputfile
aa:11:yes
bb:22:yes
cc:33:yes
dd:44:no
ee:55:yes
the inputfile is the same that output. The file will be rewritable. I wanna edit the line, change the Yes to No or No to Yes and save.
ResultNow: it find the name, changes but dont save the file.
use -i option in perl.
perl -i -wlanF'\:' -e '$F[2] =~ s/yes/no/i or $F[2] =~ s/no/yes/i if $F[0] eq "cc" ;$"=":" ; print "@F" ; ' infile.txt
and then you will find that infile.txt had been changed.
BR
I dont want to give the cc from code. The cc must be a variable, like $name. If $name exists, then find the line that $name is and edit & save.
Just for the fun of it, we can use the old unix editor "ed".
ed filename <<EOF
/^cc:
s/no/yes/
w
q
EOF
easy:-
echo "Kindly insert the name:- "
read name
perl -i -wlanF'\:' -e '$F[2] =~ s/yes/no/i or $F[2] =~ s/no/yes/i if $F[0] eq "'$name'" ;$"=":" ; print "@F" ; ' infile.txt
:p:cool:
BR
ed $c <<EOF
/^$name:
s/No/Yes/
w
q
/v12.sh: line 439: warning: here-document at line 291 delimited by end-of-file (wanted `EOF')
/v12.sh: line 440: syntax error: unexpected end of file
This solution is much more as you need, previous awk solutions are enough.
This is example template how to parse csv style file using first line = name of variables.
After parsing you can use column value using variable name. Of course you need some error checking and so on, but some simple template to start.
#!/bin/ksh or bash
cat<<EOF > $0.txt
name:age:girlfriend
aa:11:yes
bb:22:yes
cc:33:no
dd:44:no
ee:55:yes
ff:44:
:44:no
aa:55:no:xx:zz
EOF
lastfld=0
flds[0]=""
val[0]=""
oifs="$IFS"
deli=":"
#####################################
setvar()
{
str="$*"
IFS="$deli"
val=($str)
IFS="$oifs"
last=${#val[@]}
f=0
while (( f<=last && f<=lastfld ))
do
var="${flds[$f]}"
value="${val[$f]}"
eval $var=\"$value\"
((f+=1))
done
}
#####################################
showvar()
{
f=0
outstr=""
de="$deli"
while ((f<=lastfld))
do
var="${flds[$f]}"
eval value=\""\$$var"\"
(( f == lastfld )) && de=""
outstr="$outstr$value$de"
((f+=1))
done
echo "$outstr"
}
#####################################
clrvar()
{
f=0
while (( f <= lastfld ))
do
var="${flds[$f]}"
eval $var=\"\"
export $var
((f+=1))
done
}
#####################################
colnames()
{
str="$*"
IFS="$deli"
flds=($str)
IFS="$oifs"
lastfld=${#flds[@]}
((lastfld-=1))
}
#####################################
# MAIN #
lineno=0
cat $0.txt | while read line
do
((lineno+=1))
(( lineno == 1 )) && colnames "$line" && continue
clrvar # clear all variables before new values
setvar "$line"
# after line parsing you can use variables
case "$name" in
cc) girlfriend=yes ;;
esac
showvar
done
Keywords: csv ksh bash csv parser
DONE!!!! Thanks
---------- Post updated at 10:36 AM ---------- Previous update was at 10:34 AM ----------
Wow GREAT! I will use this for another porpose in my program thanks!
@rafazz Needs to be in a shell script and the string "EOF" terminates the Here Document. In your test I think that the trailing "EOF" was missing.
#!/bin/sh
ed filename <<EOF
/^cc:
s/no/yes/
w
q
EOF