sed search multiple strings in one file [closed] - linux

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I want to search multiple strings in the same file
So far I have this working but one string only
sed -n '/XXX/,+1p' FILE > FILE
But I want
sed -n '/XXX/YYY/ZZZ/,+1p' FILE > FILE
I could not got it workin

Use \| to separate multiple patterns to match.
sed -n '/XXX\|YYY\|ZZZ/,+1p' INFILE > OUTFILE
Also, the input file has to be different from the output file (if you want to overwrite the file you should use the -i option rather than redirecting to the input file).

Related

The terminal shows me nothing gives unlimited space to write when i use grep function with file name in linux terminal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I used this command line to see what is inside grep command but it gives unlimited space to write. Why is this happening grep file.txt
It is finding instances of the literal text file.txt from standard input, aka your keyboard input.
If you want to search a file, use grep PATTERN -f FILE
Check out the grep man page for more details.
Plus, the entire point of grep is to search for a pattern, so you'll need that too.

Create a shell script with a shell script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm creating a script which create file and insert content using
cat > /etc/file <<END
FILE CONTENT
END
It works for most files but it doesn't work when file content have shell commands in it.
I tried with the echo command but i have the same problem.
Why does it execute commands ?
The file's content includes $variables wich are expanded. To avoid variable expansion, I had to use single-quote escapes 'END'.

How to print difference between two files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have 1 file where data gets added every 10 min, I want to get updated data which can be stored in new file(inc1.txt) through script.
My path for file as /home/asda/Desktop/inc.txt
How this can be achive?
Use tac to cat the file backwards, and quit when you get to your marker:
tac /home/asda/Desktop/inc.txt | sed /Marker/q | tac
then add a new Marker at the end to remember where you last finished
echo "Marker" >> /home/asda/Desktop/inc.txt
This has the disadvantage that it alters your file, but you can grep out the markers when you use the file like this:
grep -v Marker /home/asda/Desktop/inc.txt
Of course, you should make the marker something that doesn't naturally occur in your file.

rename all files in folder to numbered list 1.jpg 2.jpg [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
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.
Improve this question
I have a folder full of images with several different random file names to help organize this mess I would like to, in one command rename all of them to a sequential order so if I have 100 files it starts off naming the first file file-1.jpg file-2.jpg etc. Is this possible in one command?
The most concise command line to do this I can think of is
ls | cat -n | while read n f; do mv "$f" "file-$n.jpg"; done
ls lists the files in the current directory and cat -n adds line numbers. The while loop reads the resulting numbered list of files line by line, stores the line number in the variable n and the filename in the variable f and performs the rename.
I was able to solve my problem by writing a bash script
#!/bin/sh
num=1
for file in *.jpg; do
mv "$file" "$(printf "%u" $num).jpg"
let num=$num+1
done

Replacing one number in a text and cloning it with a new value [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 need to change some numbers in a rule, like 192.168.1.2 to 192.168.1.x, where x is a value between 3-254. So'll have multiple lines, one with the value 192.168.1.2, other with the value 192.168.1.3, and so on.
Well, I've no clue how to do that.
If someone know of a program or some way to do using a script in linux, please let me know.
Here's one way using GNU sed. Simply replace X with the value you wish to use.
sed 's/\(\([0-9]\{1,3\}\.\)\{3\}\)[0-9]\{1,3\}/\1X/g' file.txt
Test:
echo "192.168.1.4" | sed 's/\(\([0-9]\{1,3\}\.\)\{3\}\)[0-9]\{1,3\}/\1X/g'
Result:
192.168.1.X
Another option, using awk to avoid the hairy regex route (nothing wrong with regexes in general, but sometimes they can make your eyes bleed...):
awk -F. '{printf "%s.%s.%s.x\n",$1,$2,$3}'

Resources