Add suffix to each lines containing specific word
Try this:
#! /bin/bash
exec < file1
while read -r id; do
check=$(grep "^$id:" file2)
if (($? == 0)); then
echo "$check"
else
echo "$id"
fi
done
Update: alternative implementation, which reads file2 just once.
#! /bin/bash
file2=$(grep ':Checked$' file2)
exec < file1
while read -r id; do
check=$(grep "^$id:" <<< $file2)
if (($? == 0)); then
echo "$check"
else
echo "$id"
fi
done
Add prefix to each word of each line in bash
With sed :
$ deps='word1 word2'
$ echo "$deps" | sed 's/[^ ]* */prefix-&/g'
prefix-word1 prefix-word2
Add a prefix string to beginning of each line
# 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/'
Add prefix and suffix to $@ in bash
I would use shell [ parameter expansion ] for this
$ set -- one two three
$ echo "$@"
one two three
$ set -- "${@/#/pre}" && set -- "${@/%/post}"
$ echo "$@"
preonepost pretwopost prethreepost
Notes
- The
#
matches the beginning - The
%
matches the end - Using double quotes around
${@}
considers each element as a separate word. so replacement happens for every positional parameter
Add prefix/suffix to all elements in space-separated string
If you have a proper array,
foo=(a b c)
you can add a prefix using the /#
operator and add a suffix with the /%
operator. It does have to be done in two steps, though.
$ foo=(a b c)
$ foo=("${foo[@]/#/__PREFIX__}")
$ foo=("${foo[@]/%/__SUFFIX__}")
$ declare -p foo
declare -a foo=([0]="__PREFIX__a__SUFFIX__" [1]="__PREFIX__b__SUFFIX__" [2]="__PREFIX__c__SUFFIX__")
If you just have a space-separated string, you can use //
:
$ foo="a b c"
$ foo="__PREFIX__${foo// /__SUFFIX__ __PREFIX__}__SUFFIX__"
$ echo "$foo"
__PREFIX__a__SUFFIX__ __PREFIX__b__SUFFIX__ __PREFIX__c__SUFFIX__
How to append suffix to all string matched regular expression in unix
Sed should do that:
sed -i~ -e 's/:\([0-9]\{1,\}\)/:\1_suffix/g' file
^ ^ ^ ^ ^ ^
| | | | | |
start capture | | end | globally, i.e. not just the first
group | | capture | occurrence on a line
any digit | the first capture
one or group contents
more times
If -i
is not supported, just create a new file and replace the old one:
sed ... > newfile
mv oldfile oldfile~ # a backup
mv newfile oldfile
Add prefix and suffix to all lines in all files?
Try doing this :
sed 's@^@<url><loc>http://www.mysite.com/review/@; s@$@</url></loc>@' files*.html
Add the -i
switch to sed
if you want to replace the files in place.
In Bash, how do I add a string after each line in a file?
If your sed
allows in place editing via the -i
parameter:
sed -e 's/$/string after each line/' -i filename
If not, you have to make a temporary file:
typeset TMP_FILE=$( mktemp )
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename
Related Topics
Sending Keycode to Xorg + Wine with Bash Script
Jni Issue on Linux: Cannot Open Shared Object File
How to Store Linux Command Output into a Variable in Puppet
How to Fetch the Tags for Ec2-Describe-Instances in a Shell Script
How to Install the Specific Version of Postgres
Does the Linux Scheduler Prefer to Run Child Process After Fork()
What Does Grep -Po '...\K...' Do? How Else Can That Effect Be Achieved
Why Does Docker Prompt "Permission Denied" When Backing Up the Data Volume
How to Add an User and Re Set the Root User in Yocto
Visual Studio Code Debugger Error:"Could Not Find the Task 'Gcc Build Active File'
Addresses of Thread Local Storage Variables
Split Delimited File into Smaller Files by Column