Remove one line only with matching string - sed [closed] - sed

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 7 years ago.
Improve this question
I have a csv file that has a number of duplicates, I want to remove only one line (or the first line) that matches my string.
How to do this in sed?

With GNU sed:
sed '0,/string/{/string/d}' file
If you want to edit "in place" add option -i.
See: How do I match only the first occurrence of a pattern?

This might work for you:
sed -n '/yourString/{:a;n;p;ba};p' file
An alternative:
sed '/yourString/{x;/./{x;b};x;h;d}' file

Related

Powershell solution required [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 3 days ago.
Improve this question
I have many text files in directory and sub directories. I want to add a Tab character before Device_Id but line must not contain Sender_Ip.
Device_Id is present in both lines either it's having Sender_Ip or not. I could replace Tab character but had replaced in both types of lines whereas I just want it to be replaced where Sende_Ip is not present.

What is the convention for line wrapping in command-line instructions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I need to document some very long command line instructions. What convention should I use to indicate that the line is not broken?
For example, for the command:
fab --fabfile=create_virtualenv.py --hosts=<user#remote> create_virtualenv:base=<base_folder>,name=<ve_name>,requirements=<requirements_file>,packages=<package_folder>
I was thinking:
fab --fabfile=create_virtualenv.py --hosts=<user#remote> \\
create_virtualenv:base=<base_folder>,name=<ve_name>,\\
requirements=<requirements_file>,packages=<package_folder>
but that's just me making something up based on a half-forgotten example, and I can see potential for ambiguities regarding whitespace and punctuation.
A referenced standard would be best, or consistent, well-regarded implementation.
On Windows, I would use the same character that CMD.EXE uses for line continuation - the caret ^.
fab --fabfile=create_virtualenv.py --hosts=<user#remote> ^
create_virtualenv:base=<base_folder>,name=<ve_name>,^
requirements=<requirements_file>,packages=<package_folder>
See Long commands split over multiple lines in Windows Vista batch (.bat) file for more info. Be sure to read the first three answers, as they each have useful information.
On 'nix, I would use a single \ for the same reason. A double \\ would imply a backslash literal, so that is not good. But a single \ at the end implies line continuation.
fab --fabfile=create_virtualenv.py --hosts=<user#remote> \
create_virtualenv:base=<base_folder>,name=<ve_name>,\
requirements=<requirements_file>,packages=<package_folder>

Why is case insensitive search and replace not working in Solaris sed? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I tried the following commands in Solaris sed for case insensitive find and replace
sed s/TOFIND/REPLACE/gi fileName
sed s/TOFIND/REPLACE/gi fileName
/usr/xpg4/bin/sed s/TOFIND/REPLACE/gi fileName
/usr/xpg4/bin/sed s/TOFIND/REPLACE/gi fileName
but none of the ways worked. I got command garbled error for all. Is there no support for case insensitive search in Solaris sed?
i is a non standard GNU sed extension.
You can use GNU sed if installed. It might be in /usr/sfw/bin/gsed or /usr/gnu/bin/sed depending on the version.
Otherwise, the standard way is
sed 's/[Tt][Oo][Ff][Ii][Nn][Dd]/REPLACE/g' fileName
You might automatize the process that way:
pattern="tofind"
sed "s/$(printf "%s" "$pattern"|sed 's/./\[\U&\L&\]/g')/REPLACE/g" fileName
another alternative is to replace each aphabetic char of the search pattern by his equivalent [sC] like this: by [tT][hH][iI][sS]: (with a previous sed/awk on pattern to be generic)
printf "%s\n" "SearchPattern" | sed 's/[aA]/[aA]/g;s[bB]/[bB]/g; ..... ;s/[zZ]/[zZ]/g' | read -r CaseSearchPattern
/usr/xpg4/bin/sed "s/${CaseSearchPattern}/REPLACE/g" fileName
just add a second test eventually (and corrective action) if some special char like \ are in the content due to "" shell interpretation arround the sed action

how to write a LaTeX macro for conditional compiling? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I want to write some notes in the draft of a LaTeX document, and at a later point I want to compile the document without them. I saw someone doing something like the following (but I forgot what he did). Write notes as {\scriptsize some_text}, and in the end replace all {\scriptsize ...} with {} with a \newcommand. But I can't figure out how to write a \newcommand to replace all {\scriptsize some_text} occurrences by empty strings. I can then just comment or uncomment the \newcommand line.
Declare in the beginning of your file
\somevariabletrue
%\somevariablefalse
so you can quickly uncomment one and comment second, and in the later part of your code:
\ifsomevariable
...
\else
...
\fi

Searching through Stackoverflow.com from the commandline/bash [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
As the title says i am looking for a way to search through stackoverflow.com using only the command line specifically bash in linux.
Things I need to accomplish :
I just need to get the top 10 answers for my question or even top 5.
Plain Text Output , i.e. strip
HTML out if possible.
Also I would prefer if you didnt give a answer that required elinks or something similar.
here's a rough script to run from command line that does the job
wget 'http://stackoverflow.com/feeds/tag?tagnames=command-line&sort=newest' -qO - | perl -nle ' print $1 if /\<title[^>]+\>([^<]*)/;'|head
It grabs RSS output for a given tag (command-line here) and sort of parses it.
To be done properly one would probably want to parse XML in a better way or use some perl rss parser.