I have done this way to read text file by using help from these : Split string into an array in Bash and How do I split a string on a delimiter in Bash? and trying to split string but i am getting only 1st line with this code: (i have pasted the same code in "file" which is i am reading.)
#!/bin/bash
i=0
for file in $(ls -l)
do
if [ -f $file ] #checking for regular file
then
if [ $file == Q1 ] || [ $file == Q1~ ]
then
continue
else
if [ -f $file ]
then
echo $file
v=$(<$file)
# echo ${v[0]} # prints all
#------------------ split string-----------------------
OIFS="$IFS"
IFS='' # split by spaces
read -a array <<< "${v}"
IFS="$OIFS"
echo ${array[0]} #
#printf "\n"
# sed -i 's/$1/$2/g' "$file"
else
echo "Error: Not found"
fi
#i=$i+1
#echo $i
fi
fi
done
exit 0
but when using with loops, then there is no splitting (2nd code)
#!/bin/bash
i=0
for file in $(ls -l)
do
if [ -f $file ] #checking for regular file
then
if [ $file == Q1 ] || [ $file == Q1~ ]
then
continue
else
if [ -f $file ]
then
echo $file
v=$(<$file)
# echo ${v[0]} # prints all
#------------------ split string-----------------------
OIFS="$IFS"
while IFS='' read -a array;
do
for i in "${array[#]}";do
echo ${array[1]} #
done
done <<< "$v"
#printf "\n"
# sed -i 's/$1/$2/g' "$file"
else
echo "Error: Not found"
fi
#i=$i+1
#echo $i
fi
fi
done
exit 0
Related
I have this shell script. And in this script i am trying to find a character at a specific position in every line and if at that position the caharacter is found then replace it with "X"
filename = "/ray/test/raymond/test2.data"
if [ -r $filename ]; then
echo " File Found..."
echo " "
fi
cat $filename |
while read line
do
var = `echo $line | awk '{print substr($0, 422, 1)}'`
if [ "$var" = "D" ]; then
`echo $line | awk '{start = substr ($0, 1, 421); end = substr ($0, 423); print start "X" end}'` >> new_file
else
`echo $line` >> new_file
fi
done
But i am getting following error while executing the script:
/ray/test/raymond$ ksh ray2.sh
ray2.sh[3]: filename: not found
File Found...
ray2.sh[14]: b: not found
ray2.sh[14]: b: not found
ray2.sh[14]: b: not found
and i am not sure why. And this i am doing in solaris 10
Well, I'll show 2 examples hopefully they can give an idea.
Example 1
filename = "/ray/test/raymond/test2.data"
if [ -f "$filename" -a -r "$filename" ]; then
while read -r line; do
if [ "${line:421:1}" = 'D' ]; then
printf '%s%s%s\n' "${line:0:420}" 'X' "${line:422}" >> "$new_file"
else
printf '%s\n' "$line" >> "$new_file"
fi
done < "$filename"
elif [ ! -f "$filename" ]; then
echo 'ERROR: Not a file'
return 1
elif [ ! -r "$filename" ]; then
echo 'ERROR: Can not read the file'
return 1
fi
Example 2
filename = "/ray/test/raymond/test2.data"
new_file="$filename"'_new.txt'
if [ -f "$filename" -a -r "$filename" ]; then
output="$new_file" awk 'BEGIN { output=ENVIRON["output"] }
{
if(substr($0, 422, 1) == "D") {
$0=sprintf("%s%s%s", substr($0, 1, 421), "X", substr($0, 423))
}
print $0 >> output
}' "$filename"
elif [ ! -f "$filename" ]; then
echo 'ERROR: Not a file'
return 1
elif [ ! -r "$filename" ]; then
echo 'ERROR: Can not read the file'
return 1
fi
So I'm making a shell script in Ubuntu. It's purpose is simple. You give a command with arguments and you get a different operation each time. The problem is that when I run the the script it won't actually run because of a syntax error in one elif. The most suspicious thing is that I have a similar elif above wich works or at least doesn't pop a syntax error...
I'm leaving my code for you to see it and understand. Thanks in advance!
if [ "$1" = "-a" -a $# -lt 3 ]
then
echo "Add a new line in katalogos!"
read -p "Give me a name... " name
read -p "Give me a surname... " surname
read -p "Give me a city name... " cityName
read -p "Give me a phone number... " num
echo "$name $surname $cityName $num" > katalogos
elif [ "$1" = "-l" -a $# -lt 3 ]
then
echo "Content of katalogos will be sorted numerically and blank lines will be excluded!"
sort -b -n katalogos
elif [ "$1" = "-s" -a $# -lt 4 ]
if [[ $2 != *[!0-9]* ]]
then
echo "Content of katalogos will be sorted according to the second argument!"
sort +$3 katalogos
fi
elif [ "$1" = "-c" -a $# -lt 4 ] // syntax error
if [[ $2 = *[!0-9]* ]]
then
echo "Content of katalogos will be sorted according to the keyword!"
if [ $(grep -e "$2" katalogos | wc -l) -eq 0 ]
then
echo "String is not matched."
else
grep -e "$2" katalogos
fi
fi
elif [ "$1" = "-d" -a ( "$3" = "-b" -o "$3" = "-r" ) ]
if [[ $2 = *[!0-9]* ]]
then
echo "Katalogos's string matching lines will be deleted and blank lines will be in their place, assuming that the third argument equals -b, else just the lines will be deleted!"
if [ $(grep -e $2 katalogos | wc -l) -eq 0 ]
then
echo "String is not matched."
else
if [ "$3" = "-b" ]
then
sed -i "$3" katalogos | sed -i '$ a '
echo "A blank line inserted in place of the deleted one."
else
sed -i "$3" katalogos
echo "Line deleted."
fi
fi
fi
elif [ "$1" = "-n" ]
echo "katalogos's number of blank lines will be shown with the ability to delete them!"
grep -cvP '\S' katalogos
read -p "Do you want to delete them? Type 1 for yes or 0 for no... " ans
if [ $ans -eq 1 ]
then
grep -cvP '\S' file | sed -i
echo "Lines deleted."
fi
else
echo "Help centre!"
echo "-Type ./telcat -a to insert a new line to katalogos."
echo "-Type ./telcat -l to see the contents of katalogos sorted numerically (excluding blank lines)."
echo "-Type ./telcat -s plus a number to see the contents of katalogos sorted by the data that the number points to."
echo "-Type ./telcat -c plus a keyword to see only the lines that match with the word given."
echo "-Type ./telcat -d plus a keyword and -b or -r to delete the lines that contain the word given. Specifically if the third argument is -b it will automatically add a blank line to the deleted one and if it is -r it will not."
echo "-Type ./telcat -n to see the number of blank lines of katalogos."
echo "End of help centre!"
fi
Can anyone help me with the below. I do not understand what is wrong, Not getting any output. My requirement is to read a file and check if it's not empty and to print the content line by line.
#!/bin/ksh
echo " enter file name "
read $file
if [ -f "$file" ] && [ -s "$file" ]
then
echo " file does not exist, or is empty "
else
while IFS='' read -r line || [[ -n "$file" ]];do
echo "$line"
done
fi
read $file should be read file
Your comparison logic is backwards.
The comparison if [ -f "$file" ] && [ -s "$file" ] is 'if the file is a regular file and not empty go into error case'. You want 'if the file is not regular or the file is empty go into error case' if [ -f "$file" ] -eq 0 || [ -s "$file" ] -eq 0.
Per ksh file read should be
while IFS='' read -r line
do
echo "$line"
done < "$file"
Further Reading On ksh redirection
I've got a file that has a json string on each line of the file
I'd like to save each line of that file to a separate json file, and optionally run a command on each line before it's saved to a file.
How can I do this?
The following will do this
save_each_line_of_file_to_a_file.sh /tmp/dump.json
save_each_line_of_file_to_a_file.sh
#!/usr/bin/env bash
function show_help()
{
IT="
usage: FILE {OUTPUT_PREFIX} {OUTPUT_POSTFIX} ${OUTPUT_FOLDER}
e.g.
/tmp/myfile -> generates files in pwd, 1,2,3,4, etc for each line
/tmp/myfile OUT json -> generates files in pwd, OUT1.json,OUT2.json,OUT3.json,OUT4.json, etc for each line
"
echo "$IT"
exit
}
if [ "$1" == "help" ]
then
show_help
fi
if [ -z "$1" ]
then
show_help
fi
OUTPUT_PREFIX=${2:-}
OUTPUT_POSTFIX=${3:-}
OUTPUT_FOLDER=${4:-}
EXTRA_CMD=${5:-}
FILE=${1:-/dev/stdin}
INDEX=1
while read line
do
OUTPUT_FILENAME="${OUTPUT_FOLDER}${OUTPUT_PREFIX}$INDEX${OUTPUT_POSTFIX}"
echo $OUTPUT_FILENAME
INDEX=$((INDEX+1))
if [ -z "$EXTRA_CMD" ]
then
echo $line > $OUTPUT_FILENAME
else
echo $line | $EXTRA_CMD > $OUTPUT_FILENAME
fi
done < "$FILE"
echo "$IT"
exit
}
if [ "$1" == "help" ]
then
show_help
fi
if [ -z "$1" ]
then
show_help
fi
OUTPUT_PREFIX=${2:-}
OUTPUT_POSTFIX=${3:-}
OUTPUT_FOLDER=${4:-}
EXTRA_CMD=${5:-}
FILE=${1:-/dev/stdin}
INDEX=1
while read line
do
OUTPUT_FILENAME="${OUTPUT_FOLDER}${OUTPUT_PREFIX}$INDEX${OUTPUT_POSTFIX}"
echo $OUTPUT_FILENAME
INDEX=$((INDEX+1))
if [ -z "$EXTRA_CMD" ]
then
echo $line > $OUTPUT_FILENAME
else
echo $line | $EXTRA_CMD > $OUTPUT_FILENAME
fi
done < "$FILE"
With the help of the community I put the following script together that adds x digit random prefix to all files. The problem is that sometimes I need to add a new file and thats when my script get in trouble
#!/bin/bash
# validate input
[ -n "$1" ] || {
printf "error: insufficient input. Usage: %s /path/to/files\n" "${0//\//}"
exit 1
}
# validate directory
[ -d "$1" ] || {
printf "error: directory not found: '%s'\n" "$1"
exit 1
}
path="$1"
## removing prefix
count=$(ls $path | cut -c1-10 | uniq | wc -l)
echo $count
echo $path
if [ $count = "1" ]
then
cd $path
for i in *; do mv "$i" `echo $i|cut -c11-`; done
echo "PREFIXES REMOVED"
else
echo "PREFIXES ARE NOT UNIQUE, SKIPPING"
fi
mask=$RANDOM$RANDOM$RANDOM; let "mask %= 10000000000";
len=$(echo ${#mask})
## Note: this assumes you are exporting mask earlier. If not, set mask here
## add validation for length 10
if [ $len -lt 10 ]; then
echo "mask is not 10" >&2
exit 1
fi
# move files
prefixcheck=$(ls $path | cut -c1-10 | uniq | wc -l)
if [ $prefixcheck != 1 ]
then
for i in "${path}"/*; do
[ -f "$i" ] || continue # if not file, skip
dir="${i%/*}" # path component
ffname="${i##*/}" # full filename component (with .ext)
mv "$i" "${dir}/${mask}${ffname}"
echo $mask " applied at " $(date) "in " $path >> /home/newprefix
done
else
echo "prefixes exist, skipping"
fi
Let's say I have 20 files with prefixes and i put a new file recording001.mp4 in the folder then my script will fail. How do I tell it to strip prefixes from all files where characters 1-10 in filename is numeric?
To remove the 10-digit prefix from the file names:
cd "$path" &&
for i in [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*
do
mv "$i" "${i#??????????}"
done