2

I would need to edit the values in a file delimited by colons (:)

I would like to replace the contents of the file by using regular expression. After a grep to confirm that the item exist in the file system. I would like to edit the contents inside it by using sed.

I am open to the use of others tools. That would help me.

EDIT:

So far i have been trying to use sed to modify the contents in the text file in test.txt. The current solution i could think of is sed.

 sed -i "s/^\($newEvent:$newPerson:[^:]*\):[^:]*:/\1:$newQty:/" test.txt 
 echo "item Qty has been updated successfully!"

Is there a way i am able to edit the content after a string matching of event && person and modify the contents of that line which matches.

Before any input content in text file

Birthday Party:John Harris:1:10:1

Enter Event: Birthday Party
Enter Person: John Harris


a)  Update Event
b)  Update Person
c)  Price
d)  Update Item brought
e)  Update Quantity Sold 
f)  Back to main menu
Please enter your choice: d
New Qty Sold : 38
Qty Sold  has been updated successfully!

a)  Update Event
b)  Update Person
c)  Update Item brought
d)  Update Quantity Sold
e)  Back to main menu
Please enter your choice: f

After input content in text file

 Birthday Party:John Harris:1:10:1
  • What file? What values? Please show us an example of your input and desired output. – terdon Jan 28 '14 at 16:46
  • Sorry but it is still not clear. What is in test.txt? What is this "last item" which you "cant seem to successfully change"? Also,please explain what you're actually trying to do, what's the big picture? – terdon Jan 28 '14 at 17:40
  • I don't know whether this is for an exercise and I don't want to assume; but if this is for actual production work, I think you should consider using a database rather than a flat plaintext file. – Joseph R. Jan 28 '14 at 17:45
  • @JosephR. yes this is a exercise which i am currently stuck with. I would have use sqlite if this is a actual production env – Len Jan 28 '14 at 17:47
  • 1
    @JosephR. thanks for your input i have specified in the question. – Len Jan 28 '14 at 17:55
1

It is unclear what exactly it is you want, but how about using read or awk?

echo -e "Happy Birthday:Mom Annie:12.99:197:101\nMeow Meow:Dad:11.11:1:1" | while IFS=: read -r event person date num1 num2 _ ; do echo $person; done

echo -e "Happy Birthday:Mom Annie:12.99:197:101\nMeow Meow:Dad:11.11:1:1" | awk -F: '$1 ~ /Happy.*/{print $1, $3, $NF}'
echo -e "Happy Birthday:Mom Annie:12.99:197:101\nMeow Meow:Dad:11.11:1:1" | awk -F: '$1 ~ /Happy.*/{print $0}'

Relevant: http://www.pement.org/awk/awk1line.txt

Consider rephrasing your question by showing the input and desired output.

  • Sorry for not being clear i have edited my question. Thanks alot for the help. – Len Jan 28 '14 at 15:43
0

For more complex text munging tasks consider using e.g. Perl or Python, or even awk (presumably in the base install of your system). sed(1) is fine as far as it goes, but it's syntax is arcane (and more suited for one-shot tasks, IMVHO).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.