I have a file as below:
line1
line2
line3
And I want to get:
prefixline1
prefixline2
prefixline3
I could write a Ruby script, but it is better if I do not need to.
prefix
will contain /
. It is a path, /opt/workdir/
for example.
I have a file as below:
line1
line2
line3
And I want to get:
prefixline1
prefixline2
prefixline3
I could write a Ruby script, but it is better if I do not need to.
prefix
will contain /
. It is a path, /opt/workdir/
for example.
# If you want to edit the file in-place
sed -i -e 's/^/prefix/' file
# If you want to create a new file
sed -e 's/^/prefix/' file > file.new
If prefix
contains /
, you can use any other character not in prefix
, or
escape the /
, so the sed
command becomes
's#^#/opt/workdir#'
# or
's/^/\/opt\/workdir/'
sed
for lightweight tasks such as this. If "prefix" is known, it's very easy to pick a character not from "prefix". –
Hesson sed
in a pipeline, e.g. foo | sed -e 's/^/x /' | bar
. –
Bursa first line (header)
? –
Selectee sed -e '1!s/^/prefix/' file
. –
Selectee sed -e '2,$s/^/prefix/'
. –
Hesson sed "s%^%$PREFIX%" file
(using double quotes to enable variable interpolation; and %
as an alternative delimiter, on the assumption that $PREFIX
might contain slashes, but won't contain percent signs). –
Intolerant /
like \/
(in single-quoted strings) or \\/
(in double-quoted strings) –
Transform sed -e 's/$/postfix/' file
if you want to add string to the end of each line. –
Trumaine perl -ne $| = 1;print "prefix$_"
. In my test this was ~80 times faster than sed. For more see this this blogpost rc3.org/2014/08/28/surprisingly-perl-outperforms-sed-and-awk by twitter.com/rafeco –
Brittbritta /
as the delimiter with sed
, you can use something like s.^./.
to add a slash at the beginning of each line. You can use any character as a delimiter. –
Transform /
. –
Transform prefix
contains /
, you can use any other character not in prefix
", followed by an example without the /
. –
Hesson -e
out of habit, because sometimes I want more than one command in the same sed
invocation. In this case, g
at the end won't really do anything, since the pattern can only match once per line. –
Hesson sed '/^prefix/!s/^/prefix/g' file
–
Hesson awk '$0="prefix"$0' file > new_file
In awk the default action is '{print $0}'
(i.e. print the whole line), so the above is equivalent to:
awk '{print "prefix"$0}' file > new_file
With Perl (in place replacement):
perl -pi 's/^/prefix/' file
prtinf "$VARIABLE\n" | awk '$0="prefix"$0'
–
Location awk
reports awk: out of memory in readrec 1 source line number 1
, but the solution with sed
completes successfully. –
Fluoroscope You can use Vim in Ex mode:
ex -sc '%s/^/prefix/|x' file
%
select all lines
s
replace
x
save and close
:%s/^/prefix/
, since this strategy ends up being useful in many situations –
Revenge If your prefix is a bit complicated, just put it in a variable:
prefix=path/to/file/
Then, you pass that variable and let awk deal with it:
awk -v prefix="$prefix" '{print prefix $0}' input_file.txt
Here is a oneliner solution using the ts
command from moreutils
$ cat file | ts prefix
And how it's derived step by step:
# Step 1. create the file
$ cat file
line1
line2
line3
# Step 2. add prefix to the beginning of each line
$ cat file | ts prefix
prefix line1
prefix line2
prefix line3
Note that the prefix will be space separated from the content
If you have Perl:
perl -pe 's/^/PREFIX/' input.file
Using & (the whole part of the input that was matched by the pattern”):
cat in.txt | sed -e "s/.*/prefix&/" > out.txt
OR using back references:
cat in.txt | sed -e "s/\(.*\)/prefix\1/" > out.txt
Using the shell:
#!/bin/bash
prefix="something"
file="file"
while read -r line
do
echo "${prefix}$line"
done <$file > newfile
mv newfile $file
While I don't think pierr had this concern, I needed a solution that would not delay output from the live "tail" of a file, since I wanted to monitor several alert logs simultaneously, prefixing each line with the name of its respective log.
Unfortunately, sed, cut, etc. introduced too much buffering and kept me from seeing the most current lines. Steven Penny's suggestion to use the -s
option of nl
was intriguing, and testing proved that it did not introduce the unwanted buffering that concerned me.
There were a couple of problems with using nl
, though, related to the desire to strip out the unwanted line numbers (even if you don't care about the aesthetics of it, there may be cases where using the extra columns would be undesirable). First, using "cut" to strip out the numbers re-introduces the buffering problem, so it wrecks the solution. Second, using "-w1" doesn't help, since this does NOT restrict the line number to a single column - it just gets wider as more digits are needed.
It isn't pretty if you want to capture this elsewhere, but since that's exactly what I didn't need to do (everything was being written to log files already, I just wanted to watch several at once in real time), the best way to lose the line numbers and have only my prefix was to start the -s
string with a carriage return (CR or ^M or Ctrl-M). So for example:
#!/bin/ksh
# Monitor the widget, framas, and dweezil
# log files until the operator hits <enter>
# to end monitoring.
PGRP=$$
for LOGFILE in widget framas dweezil
do
(
tail -f $LOGFILE 2>&1 |
nl -s"^M${LOGFILE}> "
) &
sleep 1
done
read KILLEM
kill -- -${PGRP}
-u
option to sed to avoid the buffering. –
Foofaraw Using ed:
ed infile <<'EOE'
,s/^/prefix/
wq
EOE
This substitutes, for each line (,
), the beginning of the line (^
) with prefix
. wq
saves and exits.
If the replacement string contains a slash, we can use a different delimiter for s
instead:
ed infile <<'EOE'
,s#^#/opt/workdir/#
wq
EOE
I've quoted the here-doc delimiter EOE
("end of ed") to prevent parameter expansion. In this example, it would work unquoted as well, but it's good practice to prevent surprises if you ever have a $
in your ed script.
Here's a wrapped up example using the sed
approach from this answer:
$ cat /path/to/some/file | prefix_lines "WOW: "
WOW: some text
WOW: another line
WOW: more text
function show_help()
{
IT=$(CAT <<EOF
Usage: PREFIX {FILE}
e.g.
cat /path/to/file | prefix_lines "WOW: "
WOW: some text
WOW: another line
WOW: more text
)
echo "$IT"
exit
}
# Require a prefix
if [ -z "$1" ]
then
show_help
fi
# Check if input is from stdin or a file
FILE=$2
if [ -z "$2" ]
then
# If no stdin exists
if [ -t 0 ]; then
show_help
fi
FILE=/dev/stdin
fi
# Now prefix the output
PREFIX=$1
sed -e "s/^/$PREFIX/" $FILE
PREFIX
contains any characters special to sed like a slash. –
Datum prefix_lines \*
–
Warfare You can also achieve this using the backreference technique
sed -i.bak 's/\(.*\)/prefix\1/' foo.txt
You can also use with awk like this
awk '{print "prefix"$0}' foo.txt > tmp && mv tmp foo.txt
You can do it using AWK
echo example| awk '{print "prefix"$0}'
or
awk '{print "prefix"$0}' file.txt > output.txt
For suffix: awk '{print $0"suffix"}'
For prefix and suffix: awk '{print "prefix"$0"suffix"}'
If you need to prepend a text at the beginning of each line that has a certain string, try following. In the following example, I am adding # at the beginning of each line that has the word "rock" in it.
sed -i -e 's/^.*rock.*/#&/' file_name
Simple solution using a for loop on the command line with bash:
for i in $(cat yourfile.txt); do echo "prefix$i"; done
Save the output to a file:
for i in $(cat yourfile.txt); do echo "prefix$i"; done > yourfilewithprefixes.txt
For people on BSD/OSX systems there's utility called lam
, short for laminate. lam -s prefix file
will do what you want. I use it in pipelines, eg:
find -type f -exec lam -s "{}: " "{}" \; | fzf
...which will find all files, exec lam on each of them, giving each file a prefix of its own filename. (And pump the output to fzf for searching.)
SETLOCAL ENABLEDELAYEDEXPANSION
YourPrefix=blabla
YourPath=C:\path
for /f "tokens=*" %%a in (!YourPath!\longfile.csv) do (echo !YourPrefix!%%a) >> !YourPath!\Archive\output.csv
© 2022 - 2024 — McMap. All rights reserved.
/
then its more easy to useawk
. – Calutron