The following code should be printing the contents of the sourceFile to targetFile, line by line, with each line having in front 'wireless-key s:', but it only prints 'wireless-key s:' to the targetFile.
#!/bin/bash
sourceFile="file1.log"
targetFile="/etc/network/interfaces"
numLines="$(wc -l < "${sourceFile}")"
counter=5
lineNumber=5
if (( counter >= "$numLines" )) || [[ ! -f "${sourceFile}" ]]; then
echo "invaild file" >82; exit 1
fi
while [ "$counter" -le "$numLines" ]; do
sed -i "${lineNumber} s/.*/wireless-key s: $(sed -n ${counter}p <<< " ${sourceFile}")/" "${targetFile}"
counter=$((counter + 1))
done
Thank you
perhaps a different approach...
awk '{print NR,"wireless_key",$0}' srcfile > dstfile
Related
How do I print a specific line of a file new.sh (line 60 in this case) and then set what is printed as a variable?
I want to make sure the new info will be printed in the correct section of the file before executing.
cat -n new.sh | sed -n '60p'
??????
??????
if [ "$DATA" ]; then
if [[ "$DATA" == "<data>" ]]; then
sed -i '60i < new data will be added here. >' new.sh;
elif [ $DATA != '<data>' ]; then
echo "Error"
else
return 0
fi
fi
You could do:
data=$(sed -n 60p new.sh)
but if the goal is just to insert the line before line 60 only if line 60 is the string <data>, it seems easier to just put that logic in sed:
sed '60 { /^<data>$/i\
<new data>
}' new.sh
Doing this doesn't give you the error message that you had before, so perhaps you want something like:
if ! awk '60 == NR && /^<data>$/{ print "<new data>"; ok = 1} 1 END{exit !ok}' new.sh; then
echo Error >&2
fi
LINE=`sed -n 60p <new.sh'
echo "$LINE"
if [ "$LINE" = "<data>" ]
then
sed -n 1,59p <new.sh
echo "some new data"
sed -n 61,$p <new.sh
fi > output.sh
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
I want to cut CSV file by column names.
Sample file:
"column A","column B","column C","column D","column E",
aaa,bbb,ccc,ddd,eee,
111,222,333,444,555
column_A:
$ cat column_A
aaa
111
column_B:
$ cat column_B
bbb
222
when I using awk but it's not success. Some time when we meet \n in "".
Like this:
aaa,ssss,"zzz
xxx"
but this should be a line what should I do?
just 1 " it can be successful
but when csv file have too may \n and too may “ on line
it's
can't be success
function isallline(){
LineNumber=$1
LineInfo=`echo "$2"|tr -d '\n'`
FileName=$3
LastLineNumber=$4
GetInfo=`echo "$LineInfo"|awk -F '"' '{print NF-1}'`
IsAl=$((GetInfo%2))
if [[ $IsAl != 0 ]]
then
LineNumber=$((LineNumber+1))
LineInfo="$LineInfo""`sed -n ''$LineNumber'p' $FileName|tr -d '\n'`"
if [[ $LineNumber -le $LastLineNumber ]]
then
isallline $LineNumber "$LineInfo" $FileName $LastLineNumber
else
echo "error with not complte'\"'"
fi
else
echo "$LineInfo" >>CSVFile
return $LineNumber
fi
}
function GetCsvFile()
{
FileName=$1
>CSVFile #-- clearn file --
i=1 #-- declare i=1 --
LasetLineNumber=`wc -l $FileName|awk '{print $1 }'`
LineNumber=0 #-- declare LineNumber=0 --
while read LINE #-- read file --
do
getinfo=`echo $LINE|awk -F '"' '{print NF-1}'` #-- get count(") --
if [[ $getinfo != 0 ]]
then
if [[ $LineNumber == 0 ]]
then
isallline $i "$LINE" $FileName LasetLineNumber #-- call function isallline --
LineNumber=$? #-- get function isallline return(LineNumber) --
elif [[ $LineNumber -lt $i ]]
then
isallline $i "$LINE" $FileName LasetLineNumber
LineNumber=$?
fi
fi
if [[ $i -gt $LineNumber ]]
then
echo $LINE >> CSVFile
fi
let i++
done < $FileName
}
Theoretically it's possible to write such regexp, which will be able to parse CSV. But here is brilliant answer why it's more or less practically impossible. You better use specific parser. Actually, almost any linux system has python installed and python includes tooling to parse CSV files
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
Following is a source code which takes in only 'files',lists the file permissions of a file and prints the output by replacing
r=READ,w-WRITE,x-EXECUTABLE.
It should also echo "User".But the My problem here is that I have replaced '-' by User but then if the file has a permission of r--x,it also prints "User" # that point.I know its not a correct way to go about it.Can anyone suggest me a better way of echoing "User".
I have also tried printing it before the loop but then it won't serve my purpose, as My program only works withe file permissions of a FILE and not any block/socket/pipe/directory/etc.
#!/bin/bash
if [ $# -lt 1 ];then
echo "USAGE: $0 file-name"
exit 1
fi
ls -l $1 | cut -c1-4 | tr "\012" "." > fp
i=1
while(($i <= 4))
do
p=`cat fp | cut -c$i`
case $p in
[dbsplc] | t) echo "not a file";
exit 1;;
-) echo "User";;
r) echo "READ";;
w) echo "WRITE";;
x) echo "EXECUTE";;
esac
((++i))
done
exit 0
Too complicated. You don't have to rely on ls at all:
#!/bin/bash
if [[ $# -lt 1 ]]; then
echo "USAGE: $(basename "$0") filename ..."
exit 1
fi
exit_status=0
for file in "$#"; do
if [[ ! -f "$file" ]]; then
echo "not a file: $file" >&2
exit_status=$(( exit_status + 1 ))
continue
fi
echo "$file:"
echo "User"
[[ -r "$file" ]] && echo "READ"
[[ -w "$file" ]] && echo "WRITE"
[[ -x "$file" ]] && echo "EXECUTE"
done
exit $exit_status
I'd just use stat -c %a and process that instead.
an exemple using awk (easily adaptable to your program)
ll |awk '{
rights=substr($1, 2, 3);
sub(/r/, "READ ", rights);
sub(/w/, "WRITE ", rights);
sub(/x/, "EXECUTE ", rights);
print rights $3
}'
Explanations :
rights=substr($1, 2, 3);
$1 contains rights of your program and we only takes the 3 first rights (user one)
sub(/r/, "READ ", rights);
Substiture "r" with READ in rights (and so on).
print rights $3
Print rights (substituated) and $3 that contains the user name.
This served my purpose,I separated the first condition into a different case-statement.:
#!/bin/bash
if [ $# -lt 1 ];then
echo "USAGE: $0 file-name"
exit 1
fi
ls -l $1 | cut -c1-4 | tr "\012" "." > fp
i=1
while(($i == 1))
do
p=`cat fp | cut -c$i`
case $p in
[dbsplc] | t) echo "not a file";
exit 1;;
esac
echo "User"
((++i))
done
while(($i <= 4))
do
p=`cat fp | cut -c$i`
case $p in
r) echo "READ";;
w) echo "WRITE";;
x) echo "EXECUTE";;
esac
((++i))
done
exit 0