sed interview questions
Top sed frequently asked interview questions
I need to repeatedly remove the first line from a huge text file using a bash script.
Right now I am using sed -i -e "1d" $FILE
- but it takes around a minute to do the deletion.
Is there a more efficient way to accomplish this?
Source: (StackOverflow)
In my bash script I have an external (received from user) string, which I should use in sed pattern.
REPLACE="<funny characters here>"
sed "s/KEYWORD/$REPLACE/g"
How can I escape the $REPLACE
string so it would be safely accepted by sed
as a literal replacement?
NOTE: The KEYWORD
is a dumb substring with no matches etc. It is not supplied by user.
Source: (StackOverflow)
I am trying to delete empty lines using sed
sed '/^$/d'
but i have no luck with it.
for example I have this lines:
xxxxxx
yyyyyy
zzzzzz
and i want it to be like:
xxxxxx
yyyyyy
zzzzzz
what should be the code for this?
Source: (StackOverflow)
I'm trying to search and replace a string in all files matched by grep on a linux machine. I've got some pieces of what I want to do, but I'm unsure how best to string them all together.
grep -n 'foo' *
will give me output in the form:
[filename]:[line number]:[text]
For each file returned by grep, I'd like replace "foo" with "bar" and write the result back to the file. Is there a good way to do that? Maybe a fancy pipeline?
Source: (StackOverflow)
How to I find and replace every occurrence of:
subdomainA.example.com
with
subdomainB.example.com
in every text file under the /home/www/
directory tree (recursive find/replace).
Source: (StackOverflow)
just want to know what are the main differences among them? and the power of each language (where it's better to use it).
Edit: it's not "vs." like topic, just information.
Source: (StackOverflow)
I have a file of id's that are comma separated. I'm trying to replace the commas with a new line. I've tried:
sed 's/,/\n/g' file
but it is not working. What am I missing?
Source: (StackOverflow)
Is there an invocation of sed
todo in-place editing without backups that works both on Linux and Mac? While the BSD sed
shipped with OS X seems to need sed -i '' …
, the GNU sed
Linux distributions usually come with interprets the quotes as empty input file name (instead of the backup extension), and needs sed -i …
instead.
Is there any command line syntax which works with both flavors, so I can use the same script on both systems?
Source: (StackOverflow)
Is there any way to tell sed
to output only captured groups? For example given the input:
This is a sample 123 text and some 987 numbers
and pattern:
/([\d]+)/
Could I get only 123 and 987 output in the way formatted by back references?
Source: (StackOverflow)
I want to delete one or more specific line numbers from a file. How would I do this using sed?
Source: (StackOverflow)
I want to programmatically edit file content using windows command line (cmd.exe). In *nix there is sed for this tasks. Is there any usefull equivalent in windows?
Edit: I am looking for native command line solution.
Source: (StackOverflow)
- What is the difference between awk
and sed ?
- What kind of application are best use
cases for sed and awk tools ?
Source: (StackOverflow)
Is there a "canonical" way of doing that? I've been using head -n | tail -1
which does the trick, but I've been wondering if there's a bash tool that specifically extracts a line (or a range of lines) from a file.
EDIT: By "canonical" I mean a program whose main function is doing that.
Source: (StackOverflow)
I run this command to find and replace all occurrences of 'apple' with 'orange' in all files in root of my site:
find ./ -exec sed -i 's/apple/orange/g' {} \;
but it doesn't go through sub directories.
what is wrong with this command?
Edited: here is some lines of output of find ./ command:
./index.php
./header.php
./fpd
./fpd/font
./fpd/font/desktop.ini
./fpd/font/courier.php
./fpd/font/symbol.php
Source: (StackOverflow)
I have some files that I'd like to delete the last newline if it is the last character in a file. 'od -c' shows me that the command I run does write the file with a trailing new line:
0013600 n t > \n
I've tried a few tricks with sed but the best I could think of isn't doing the trick:
sed -e '$s/\(.*\)\n$/\1/' abc
Any ideas how to do this?
Source: (StackOverflow)