Text substitution in file while expanding variables, using them as string literals, and adding a newline - bash

I want to replace anything that looks like
if (Thing *thing1 = Stuff) {
with
Thing *thing1 = Stuff;
if (thing1) {
Essentially take declarations out of if statements.
I have all the parts and I see that they're correct, I'm just having trouble with the replacement
grep ' if (.* = .*) {' $file | while read -r line ; do
inside="$( echo "$line" | cut -d "(" -f2 | cut -d ")" -f1 );"
object="$( echo $( echo "$inside" | cut -d "*" -f2 | cut -d "=" -f1 ) | xargs )"
newIF=" if ($object) {"
replacement="$inside\n$newIF"
line_regexp="$(echo "$line" | sed -e 's/[]\/$*.^|[]/\\&/g')"
replacement_regexp="$(echo "$replacement" | sed -e 's/[\/&]/\\&/g')"
sed -i.bak "s/$line_regexp/$replacement_regexp/g" $file
done
Edit thanks to: https://superuser.com/questions/422459/substitution-in-text-file-without-regular-expressions
Now I just have to figure out how to turn "\n" into an actual newline

This sed should work for you:
$ sed -n 's/if (\([^ ]* \*\([^ ]*\) = [^)]*\)) {/\1;\nif (\2) {/p' <<< "if (Thing *thing1 = Stuff) {"
Thing *thing1 = Stuff;
if (thing1) {

Related

How to replace a word in a specific line where the replace pattern contains a variable?

I have the following files:
[root#f9044b5d9d1e aws-marketing-and-sales]# grep -HRe ".*\/common\/.*\${local.parent_folder_name}" *
ap-northeast-1/config/config/terragrunt.hcl: inline_policy = templatefile("${get_parent_terragrunt_dir()}/common/${local.environment}/config/${local.parent_folder_name}/inline-policy-s3.tpl", {
ap-northeast-2/config/config/terragrunt.hcl: inline_policy = templatefile("${get_parent_terragrunt_dir()}/common/${local.environment}/config/${local.parent_folder_name}/inline-policy-s3.tpl", {
ap-south-1/config/config/terragrunt.hcl: inline_policy = templatefile("${get_parent_terragrunt_dir()}/common/${local.environment}/config/${local.parent_folder_name}/inline-policy-s3.tpl", {
ap-southeast-1/config/config/terragrunt.hcl: inline_policy = templatefile("${get_parent_terragrunt_dir()}/common/${local.environment}/config/${local.parent_folder_name}/inline-policy-s3.tpl", {
I'm trying to replace the occurrences of "${local.parent_folder_name}" where the line contains "common" in all files with the parent folder name, like this:
for file in $(grep -HRe ".*\/common\/.*\${local.parent_folder_name}" *); do
filename=$(echo $file | cut -d: -f1)
parent=$(echo $file | rev | cut -d/ -f2 | rev)
sed -i "/common/\${local.parent_folder_name}/$parent/g" $filename
done
This is the error that I get when running the above script:
sed: -e expression #1, char 9: unknown command: `$'
I've found some SO questions regarding this but none of them have examples with using a variable as the replace pattern.
I've also tried different separators (| , !) but to no avail.
Edit:
#moshe, it didn't work, that's the output:
grep -Re "/common\/.*\${local.parent_folder_name}" . | while read -r grep_line; do
if [[ $grep_line == *"$0"* ]]; then
continue
fi
echo $grep_line
filename=$(echo $grep_line | cut -d: -f1)
parent=$(echo $grep_line | rev | cut -d/ -f2 | rev)
echo "parent: $parent"
sed -i "/common/s?\${local.parent_folder_name}?$parent?g" $filename
done
./ca-central-1/config/config/terragrunt.hcl: inline_policy = templatefile("${get_parent_terragrunt_dir()}/common/${local.environment}/config/${local.parent_folder_name}/inline-policy-s3.tpl", {
parent: ${local.parent_folder_name}
sed: 1: "./ca-central-1/config/c ...": invalid command code .
./us-west-2/config/config/terragrunt.hcl: inline_policy = templatefile("${get_parent_terragrunt_dir()}/common/${local.environment}/config/${local.parent_folder_name}/inline-policy-s3.tpl", {
I tried replacing the "." to "8" in the first grep and it worked only on some of the files but not on all. Any idea?
What am I doing wrong?
not sure what the parent var should be, probably should fix it, but the script could look like that
#!/bin/bash
grep -re "/common\/.*\${local.parent_folder_name}" . | while read -r grep_line; do
if [[ $grep_line == *"$0"* ]]; then
continue
fi
echo $grep_line
filename=$(echo $grep_line | cut -d: -f1)
parent=$(echo $grep_line | rev | cut -d/ -f2 | rev)
echo "parent: $parent"
sed -i "/common/s?\${local.parent_folder_name}?$parent?g" $filename
done
Note that sed takes a pattern (/common/) and then a command (a to append, d to delete, s to replace)
We want to change only lines with the pattern /common/ in them,
so after the pattern, we perform a regular search and replace - s/\${local.parent_folder_name}/$parent/g
To make it more readable, I changed the separator / to ?
So the sed is:
sed -i "/common/s?\${local.parent_folder_name}?$parent?g" $filename

Shell script result output to a file

I just faced a problem about shell script output problem. Here is the code
read_each_user_rating(){
TOTAL_RATING_NUMBER="$(grep -c '<Author>' $1)" #Find how many rating in each file
ALL_AUTHOR="$(grep '<Author>' $1 | sed -e 's/<Author>//'| tr -d '\r')"
ALL_COMMENT="$(grep '<Content>' $1 | sed -e 's/<Content>//'| tr -d '\r')"
ALL_DATE="$(grep '<Date>' $1 | sed -e 's/<Date>//'| tr -d '\r')"
ALL_RATING_FILE="$(grep '<Overall>' $1 | sed -e 's/<Overall>//'| tr -d '\r')"
ALL_VALUE="$(grep '<Value>' $1 | sed -e 's/<Value>//'| tr -d '\r')"
ALL_ROOMS="$(grep '<Rooms>' $1 | sed -e 's/<Rooms>//'| tr -d '\r')"
ALL_LOCATION="$(grep '<Location>' $1 | sed -e 's/<Location>//'| tr -d '\r')"
ALL_CLEANLINESS="$(grep '<Cleanliness>' $1 | sed -e 's/<Cleanliness>//'| tr -d '\r')"
ALL_CHECKIN="$(grep '<Check in / front desk>' $1 | sed -e 's/<Check in / front desk>//'| tr -d '\r')"
ALL_SERVICE="$(grep '<Service>' $1 | sed -e 's/<Service>//'| tr -d '\r')"
ALL_BUSSINESS="$(grep '<Bussiness service>' $1 | sed -e 's/<Bussiness service>//'| tr -d '\r')"
for ((COUNTER_A=1;COUNTER_A<=$TOTAL_RATING_NUMBER;COUNTER_A++))
do
echo "INSERT INTO UserRating (Author,Comment,Date,Overall,Value,Rooms,Locations,Cleanliness,Checkin,Service,Bussiness)" >> hotelreviews.sql
echo $($ALL_AUTHOR | sed "${COUNTER_A}q;d") >> hotelreviews.sql
done
}
read_each_user_rating $1
I can output
echo "INSERT INTO UserRating (Author,Comment,Date,Overall,Value,Rooms,Locations,Cleanliness,Checkin,Service,Bussiness)" >> hotelreviews.sql to the file. But why i can output "echo $($ALL_AUTHOR | sed "${COUNTER_A}q;d") >> hotelreviews.sql" part to file too?
echo $($ALL_AUTHOR | sed "${COUNTER_A}q;d") >> hotelreviews.sql
is malformed. The expression inside $() must be a valid command (of which $ALL_AUTHOR is almost certainly not).
More likely you need something such as:
echo "$ALL_AUTHOR" | sed "${COUNTER_A}q;d"
inside the $().
In this case however, it's almost certainly not necessary to invoke a sub-command at all when you can simply do:
echo "$ALL_AUTHOR" | sed "${COUNTER_A}q;d" >>hotelreviews.sql

how to read words from a file using shell script

i have a file /ws/$1-rcd/temp.txt which has only one line as follows
198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/...
i have a script to get the value repository/open_source/commons_collections and 3_2_2 by reading the file and looping through it using for loop
i have my code as follows
grep -n "$4" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f1,2 | sed -e 's/\:element/|/g' | sed -e 's/ //g' > /ws/$1-rcd/temp.txt
for i in `cat /ws/$1-rcd/temp.txt`
do
line=`echo $i | cut -d"|" -f1`
path=`echo $i | cut -d"|" -f2`
whoami
directory_temp=`echo $path | awk -F "/" '{ print $(NF-2)}'`
if [ "$directory_temp" == "$4" ]
then
OLD_VERSION=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
total_fields=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
dir_path=`expr ${total_fields} - 2`
loc=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${dir_path}"`
location=`echo $loc | cut -d"/" -f4,5,6`
fi
done
but when i run this code it gives me an error as
-bash: line 45: syntax error near unexpected token |'
-bash: line 45:for i in 198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/...'
can anyone please suggest what am i doing wrong
If you want to iterate through each line of a file, use while loop like below
while read -r line ;do
echo $line
done <file.txt
so, your code can be rewritten as
grep -n "$4" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f1,2 | sed -e 's/\:element/|/g' | sed -e 's/ //g' > /ws/$1-rcd/temp.txt
while read i ; do
line=`echo $i | cut -d"|" -f1`
path=`echo $i | cut -d"|" -f2`
whoami
directory_temp=`echo $path | awk -F "/" '{ print $(NF-2)}'`
if [ "$directory_temp" == "$4" ]
then
OLD_VERSION=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
total_fields=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
dir_path=`expr ${total_fields} - 2`
loc=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${dir_path}"`
location=`echo $loc | cut -d"/" -f4,5,6`
fi
done < /ws/$1-rcd/temp.txt
You may be better served relying on parameter expansion and substring removal. For example:
#!/bin/sh
a=$(<dat/lline.txt) ## read file into a
a=${a##*ccm_tpl/} ## remove from left to ccm_tpl/
num=${a##*collections/} ## remove from left to collections/
num=${num%%/*} ## remove from right to /
a=${a%%${num}*} ## remove from right to $num
Input File
$ cat dat/lline.txt
198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/..
Output
$ sh getvals.sh
a : repository/open_source/commons_collections/
num : 3_2_2
If you need to trim in some other way, just let me know and I'm happy to help further.

String separation in unix shell

STRINGS=str1,str2,str3
I want each string seperated by space as given below:
STRING1=`echo $STRINGS | cut -d',' -f1` ==> gives "str1"
REMAINING_STRING=`echo $STRINGS | cut -d',' -f2- |
sed -e 's/,/ /g'` ==> gives "str2 str3"
But when the string contains only one entry, for e.g STRINGS=str1 , then REMAINING_STRING is also populating with same value as STRING1. I want REMAINING_STRING to be null when the STRINGS contain only one entry.
STRINGS=str1
STRING1=`echo $STRINGS| cut -d',' -f1` ==> gives "str1"
REMAINING_STRING=`echo $STRINGS | cut -d',' -f2- | sed -e 's/,/ /g'`
==> gives "str1", But this should come as null.
How to do this in unix shell?
$ STRINGS=str1
$ echo $STRINGS | cut -d',' -f2- | sed -e 's/,/ /g'
str1
$ echo $STRINGS | cut -s -d',' -f2- | sed -e 's/,/ /g'
$
Explanation of -s from the man page.
-s, --only-delimited
do not print lines not containing delimiters
Use the -a flag of the read command, to split the string into an array. Example:
$ cat script.sh
#!/bin/bash
strings=str1,str2,str3
IFS=, read -ra arr <<< "$strings"
echo "First element: ${arr[0]}"
echo "Second element: ${arr[1]}"
strings=str1
IFS=, read -ra arr <<< "$strings"
echo "First element: ${arr[0]}"
echo "Second element: ${arr[1]}"
$ ./script.sh
First element: str1
Second element: str2
First element: str1
Second element:
The alternative method of splitting the string with
IFS=, arr=($strings)
will also work for most strings, but will fail in there is pathname expansion, E.g. arr=(*) would match all files in the current directory (as konsolebox noted).
Extension of user000001's answer:
$ cat strings.sh
#!/bin/bash
function splitstr {
local a
set -f
IFS=, a=($1); str_first=$a; unset a[0]; str_rest="${a[#]}"
set +f
}
splitstr 'one,*,three,four'; echo -e "<$str_first>\t<$str_rest>"
splitstr 'one'; echo -e "<$str_first>\t<$str_rest>"
splitstr ''; echo -e "<$str_first>\t<$str_rest>"
$ ./strings.sh
<one> <* three four>
<one> <>
<> <>

how to list file names into a function

I have a folder with a bunch of files, the files only have a url in it i.e
http://itunes.apple.com/us/app/keynote/id361285480?mt=8
Here is my code. How can I get it to do this for each url in each file?
var='{"object":"App","action":"scrape","args":{"itunes_url":"!!!!HERE!!!!"}}'
string=$(echo "$var" | sed -e 's/"/\\"/g')
string='{"request":"'"$string"'"}'
api="http://api.lewis.com"
output=$(curl -s -d "request=$string" "$api")
code=$(echo "$output" | tr '{', '\n' | sed -n "2p" | sed -e 's/:/ /' | awk '{print $2}')
if [ "${code:0:1}" -ne "2" ]; then
# :(
echo "Error: response code $code was returned, "
else
string=$(echo "$output" | tr '{', '\n' | sed -e '/"signature":\(.*\)/d;/"data":\(.*\)/d;/"signature":\(.*\)/d;/"code":\(.*\)/d' |sed -e 's/\\"//g;s/\\\\\\\//\//g;s/\\//g' | tr '}', '\n' | sed -e 's/"//' | sed '/^$/d')
echo "$string"
fi
use a for loop
for filename in folder/*; do
-- your code where you do something using $filename --
done
og if you prefer to give the filenames as arguments to the script then:
for filename do
-- your code where you do something using $filename --
done
then run your script followed by the files
./script.sh folder/*
You could do:
for file in *; do
for line in $(cat $file); do
# Stuff goes here
done
done
Or even just:
for line in $(cat *); do
# Stuff goes here
done

Resources