how to list file names into a function - bash

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

Related

How to trim a string in shell script

I have a string,
var=refs/heads/testing/branch
I want to get rid of refs/heads/ in the string using shell script, such that I have only:
var=testing/branch
Commands I tried (one per line):
echo $(var) | awk -F\\ {'print $2'}
echo $var | sed -e s,refs/heads/,,
echo "refs/heads/testing/branch" | grep -oP '(?<=refs/heads/\)\w+'
echo "refs/heads/testing/branch" | LC_ALL=C sed -e 's/.*\\//'
echo "refs/heads/testing/branch" | cut -d'\' -f2
echo refs/heads/testing/branch | sed -e s,refs/heads/,,
there are lots of options out there ,try easy ones:
echo $var | cut -d "/" -f 3,4
echo $var | awk -F"/" '{print $3"/"$4}'
Shell parameter expansion: remove the prefix "refs/heads/" from the variable contents
$ var=refs/heads/testing/branch
$ echo "${var#refs/heads/}"
testing/branch

Bash script to stdout stuck with redirect

My bash script is the following:
#!/bin/bash
if [ ! -f "$1" ]; then
exit
fi
while read line;do
str1="[GAC]*T"
num=$"(echo $line | tr -d -c 'T' | wc -m)"
for((i=0;i<$num;i++))do
echo $line | sed "s/$str1/&\n/" | head -n1 -q
str1="${str1}[GAC]*T"
done
str1="[GAC]*T"
done < "$1
While it works normally as it should (take the filename input and print it line by line until the letter T and next letter T and so on) it prints to the terminal.
Input:
GATTT
ATCGT
Output:
GAT
GATT
GATTT
AT
ATCGT
When I'm using the script with | tee outputfile the outputfile is correct but when using the script with > outputfile the terminal hangs / is stuck and does not finish. Moreover it works with bash -x scriptname inputfile > outputfile but is stuck with bash scriptname inputfile > outputfile.
I made modifications to your original script, please try:
if [ ! -f "$1" ]; then
exit
fi
while IFS='' read -r line || [[ -n "$line" ]];do
str1="[GAC]*T"
num=$(echo $line | tr -d -c 'T' | wc -m)
for((i=0;i<$num;i++));do
echo $line | sed "s/$str1/&\n/" | head -n1 -q
str1="${str1}[GAC]*T"
done
str1="[GAC]*T"
done < "$1"
For input:
GATTT
ATCGT
This script outputs:
GAT
GATT
GATTT
AT
ATCGT
Modifications made to your original script were:
Line while read line; do changed to while IFS='' read -r line || [[ -n "$line" ]]; do. Why I did this is explained here: Read a file line by line assigning the value to a variable
Line num=$"(echo $line | tr -d -c 'T' | wc -m)" changed to num=$(echo $line | tr -d -c 'T' | wc -m)
Line for((i=0;i<$num;i++))do changed to for((i=0;i<$num;i++));do
Line done < "$1 changed to done < "$1"
Now you can do: ./scriptname inputfile > outputfile
Try:
sed -r 's/([^T]*T+)/\1\n/g' gatc.txt > outputfile
instead of your script.
It takes some optional non-Ts, followed by at least one T and inserts a newline after the T.
cat gatc.txt
GATGATTGATTTATATCGT
sed -r 's/([^T]*T+)/\1\n/g' gatc.txt
GAT
GATT
GATTT
AT
AT
CGT
For multiple lines, to delete empty lines in the end:
echo "GATTT
ATCGT" | sed -r 's/([^T]*T+)/\1\n/g;' | sed '/^$/d'
GATTT
AT
CGT

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

Shell sed command

I have paths.txt like:
pathO1/:pathD1/
pathO2/:pathD2/
...
pathON/:pathDN/
How can I 'sed' insert ' * ' after each pathOX/ ?
The script is:
while read line
do
cp $(echo $line | tr ':' ' ')
done < "paths.txt"
substituted by:
while read line
do
cp $(echo $line | sed 's/:/* /1')
done < "paths.txt"
This looks to be a similar question to which you asked earlier: Shell Script: Read line in file
Just apply the trick of removing additional '*' before appliying tr like:
cp $(echo $line | sed 's/\*//1' | tr ':' '* ')
while read line
do
path=`echo "$line" | sed 's/:/ /g'`
cmd="cp $path"
echo $cmd
eval $cmd
done < "./paths.txt"
quick and dirty awk one-liner without loop to do the job:
awk -F: '$1="cp "$1' paths.txt
this will output:
cp /home/Documents/shellscripts/Origen/* /home/Documents/shellscripts/Destino/
cp /home/Documents/shellscripts/Origen2/* /home/Documents/shellscripts/Destino2/
...
if you want the cmds to get executed:
awk -F: '$1="cp "$1' paths.txt|sh
I said it quick & dirty, because:
the format must be path1:path2
your path cannot contain special letters (like space) or :
Using pure shell
while IFS=: read -r p1 p2
do
cp $p1 "$p2"
done < file

" echo" in bash script empty file

I have problem with echo command I need export data to csv but its empty file
#!/bin/bash
while read domain
do
ownname= whois $domain | grep -A 1 -i "Administrative Contact:" |cut -d ":" -f 2 | sed 's/ //' | sed -e :a -e '$!N;s/ \n/,/;ta'
echo -e "$ownname" >> test.csv
done < dom.txt
You need to use command substitution to store command's output in a shell variable:
#!/bin/bash
while read domain; do
ownname=$(whois $domain | grep -A 1 -i "Administrative Contact:" |cut -d ":" -f 2 | sed 's/ //' | sed -e :a -e '$!N;s/ \n/,/;ta')
echo -e "$ownname" >> test.csv
done
PS: I haven't tested all the piped commands.

Resources