sed how I can replace csv only IF line contain string? [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a csv like this
titlemmmm;fff;ggg
mmmm;fff;ggg
mmmm;fff;ggg
I need to replace for obtain this
titlemmmm*fff*ggg
mmmm;fff;ggg
mmmm;fff;ggg
how I could do this
Please help me

You can use this:
sed '/title/s/;/*/g' file.txt
Or if you only want to match "title" at the beginning of a line:
sed '/^title/s/;/*/g' file.txt
If you want to edit the file in place, without needing to direct the output to a new file, you can use the -i option:
sed -i '/^title/s/;/*/g' file.txt

Related

Replace with sed on csv file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a csv file and I am trying to substitute the last letter for a word...
The input is
1111;AAA;... (more columns);A1a;A
2222;XXX;... (more columns);T3g;B
... (more rows)
...(more rows)
4564;AdA;... (more columns);G1a;A
33321;B1X; ... (more columns);T3g;B
And I want to replace A for "Avocado" and B for "Banana"...
I tried
#sed -e "s/;A$/;C/g" file.csv
But doesn't work, any advice, please?
Is the following what you're trying to achieve?
tink#host:~/tmp$ sed 's/A$/Avocado/;s/B$/Banana/' file.csv
1111;AAA;... (more columns);A1a;Avocado
2222;XXX;... (more columns);T3g;Banana
... (more rows)
...(more rows)
4564;AdA;... (more columns);G1a;Avocado
33321;B1X; ... (more columns);T3g;Banana
If that looks correct, and you want to change in-file, add a -i to sed.
If you want a new file, add a > new_file to the end of the line.
This seems to work:
sed -i 's/A$/Avocado$/g' file.csv
sed -i 's/B$/Banana$/g' file.csv
The -i replaces the text and the Regex doesn't need the ; because it should use only one character, right? Therefore, one can just specify that character and replace it with a whole word.

How do a search a text file for a list of phrases contained in another text file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a text file with many redundancies in English, one on each line, for example
in excess of
in order to
in order for
...
I would like to search another text document to see if it contains any of these phrases. If it does all it need do is print the phrase, I can do the rest manually. Can I do this easily on the command line?
sure - try grep
grep -F -f phrases.txt doc.txt
phrases.txt
in excess of
in order to
in order for
doc.txt
use grep -f in order to match this line
this line won't be matched
you can use grep -o to only print the matched phrase - not the entire line:
$ grep -o -F -f phrases.txt doc.txt
in order to

In Bash, how do I add a literal \n after each line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a file which contains URL's separated by new lines.
After each URL, I would like to insert the literal character "\n".
How can I do so?
sed -i.BAK 's/$/\\n/' files
perl -i.BAK -pe 's/$/\\n/' files
check this example:
kent$ seq 5|sed 's/$/\\n/'
1\n
2\n
3\n
4\n
5\n

inserting a certain text between in every occurrence of two following tabs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to insert NA between every occurrence of two tab characters immediately following each other in a text file. How can I do it with a sed command?
This might work for you (GNU sed):
sed ':a;s/\t\t/\tNA\t/g;ta' file
This covers all occurrances of \t\t throughout a file
Or if you prefer:
sed 's/\t\t/\tNA\t/g;s//\tNA\t/g' file
Like this:
sed 's/xx/xNAx/g' file
where you type x using Control-V TAB
Or, if you have GNU sed, you can type:
sed 's/\t\t/\tNA\t/g' file

How to add a character in front of multiple words in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is a text file containing many words,each is separated by space breaks or line breaks.
Now I want to add a character,like "#" "$" "#" in front of each of them,
and I found doing this job one by one will take too much time,
are there any better ways,in bash?
Try using sed
sed -r 's/([^ ]+)/#\1/g' file
Or more concisely,
sed -r 's/[^ ]+/#&/g' file
Sample input
abc def pqr-stu xyz
Output
#abc #def #pqr-stu #xyz
Using sed, you could say:
sed 's/\b\w/#&/g' inputfile
This would append # before every word.

Resources