Problems Writing Output to File - file

I am reading in a small csv file in the format size,name - one set per line. For my testing file I have two lines in the csv file.
If I use the code
while
IFS=',' read -r size name
do
printf "%s\n" "name"
done < temp1.txt
The name values for each of the lines is printed to the terminal.
If I use the code
while
IFS=',' read -r size name
do
printf "%s\n" "name" > temp2.txt
done < temp1.txt
Then only the last name is printed to the temp2.txt file.
What am I doing wrong?!

You are using >, so that the file gets truncated every time. Instead, use >> to append:
So it should be like this:
printf "%s\n" "name" >> temp2.txt
^^
All together:
while
IFS=',' read -r size name
do
printf "%s\n" "name" >> temp2.txt
done < temp1.txt
Basic example:
$ echo "hello" > a
$ echo "bye" > a
$ cat a
bye # just last line gets written
$ echo "hello" >> a
$ echo "bye" >> a
$ cat a
hello
bye # everything gets written

Related

Storing data in multiple arrays (bash)

I am trying to store the contents of a .txt file in two sets of arrays in bash. The file is a list of characteristics for given data files, delimited by vertical bars (|). So far, I have written code that reads the file and prints each line of data separately, each followed by the given sections of the line.
#prints line of text and then separated version
while IFS='' read -r line || [[ -n "$line" ]]
do
echo "Text read from file: $line"
words=$(echo $line | tr "|" "\n")
for tests in $words
do
echo "> $tests"
done
done < "$1"
Example output:
Text read from file: this|is|data|in|a|file
> this
> is
> data
> in
> a
> file
Text read from file: another|example|of|data
> another
> example
> of
> data
Is there a way for me to store each individual line of data in one array, and then the broken up parts of it within another? I was thinking this might be possible using a loop, but I am confused by arrays using bash (newbie).
OK -- I just read in the lines like you have done, and append them to the lines array. Then, use tr as you have done, and append to the words array. Just use the parentheses to mark them as array elements in the assignments:
$ cat data.txt
this|is|data|in|a|file
another|example|of|data
$ cat read_data.sh
#!/bin/bash
declare -a lines
declare -a words
while IFS='' read -r line || [[ -n "$line" ]]
do
echo "Text read from file: $line"
lines+=( $line )
words+=( $(echo $line | tr "|" " ") )
done < "$1"
for (( ii=0; ii<${#lines[#]}; ii++ )); do
echo "Line $ii ${lines[ii]}"
done
for (( ii=0; ii<${#words[#]}; ii++ )); do
echo "Word $ii ${words[ii]}"
done
$ $ ./read_data.sh data.txt
Text read from file: this|is|data|in|a|file
Text read from file: another|example|of|data
Line 0 this|is|data|in|a|file
Line 1 another|example|of|data
Word 0 this
Word 1 is
Word 2 data
Word 3 in
Word 4 a
Word 5 file
Word 6 another
Word 7 example
Word 8 of
Word 9 data

Storing Command Line Arguments in an Array , Arguments can be Integers OR a file containing integers delimited by a space

Requirement :
Able to send a file containing numbers (433 434 435) as a parameter
sh Test.sh myFile.txt
Parameters can be numbers directly if not a file (433 434 434)
sh Test.sh 434 435 436
So , it has to support both file and numbers as the parameters
Below is the code i ve tried writing but in the for loop below , all numbers are getting printed as a string , but i need the for loop to run thrice as the input values are 3.
How to have it as a part of an array in shell script
Iam relatively new to shell script
OutPut:
In either case for loop has to run the number of parameter times(filedata determinies the parameters or direct input)
Please advice if any unforeseen bugs exist
#!/bin/bash
echo -e $# 2>&1 ;
myFile=$1 ; // As the first parameter will be a file
#[ -f "$myFile" ] && echo "$myFile Found" || echo "$myFile Not found"
if [ -f "$myFile" ]; then
tcId=`cat $#`;
echo $tcId;
else
tcId=$#;
echo $tcId;
fi
# Execute each of the given tests
for testCase in "$tcId"
do
echo "Test Case is "$testCase ;
done
I'd use a process substitution to "pretend" the explicit arguments are in a file.
while IFS= read -r testCase; do
echo "Test case is $testCase"
done < <( if [ -f "$1" ]; then
cat "$1"
else
printf "%s\n" "$#"
fi
)
If you are flexible in how your script is called, I would simplify it to only read test cases from standard input
while IFS= read -r testCase; do
echo "Test case is $testCase"
done
and call it one of two ways, neither using command line arguments:
sh Test.sh < myFile.txt
or
sh Test.sh <<TESTCASES
433
434
434
TESTCASES

Read a file and affect the splitting result in an array with bash

My code read a file by line and split each line by a comma ; or space and the results is affected to an array, but the proble is that i can't read the elmen of the array
#!/bin/bash
filename="$1"
while read -r line
do
name=$line
echo "Name read from file - $name"
arr=$(echo $name | tr ";" "\n")
echo ${arr[1]}
for x in $arr
do
echo "> [$x]"
var1=$x
var2=$x
done
done < "$filename"
the problem is in the command:
echo ${arr[1]}
the file that i use contain line :
car; vehicle
computer;apple
To loop through an array:
for x in "${arr[#]}"; do
echo "> [$x]"
done
The ${arr[#]} expansion will include the entire array, while $arr alone only includes the first array element.
However if you use read -ra with custom IFS then you can directly read each delimited line into an array:
while IFS=';' read -ra arr; do
printf "[%s]\n" "${arr[#]}"
echo '----------'
done < file
Output:
[car]
[vehicle]
----------
[computer]
[apple]
----------

How can I assign a value to an array in Bash?

I am trying to read a list of values from a text file, hello.txt, and store them in an array.
counter=0
cat hello.txt | while read line; do
${Unix_Array[${counter}]}=$line;
let counter=counter+1;
echo $counter;
done
echo ${Unix_Array[0]}
echo ${Unix_Array[1]}
echo ${Unix_Array[2]}
I am not able to assign values to the array Unix_Array[]... The echo statement does not print the contents of the array.
There are a few syntax errors here, but the clear problem is that the assignments are happening, but you're in an implied subshell. By using a pipe, you've created a subshell for the entire while statement. When the while statement is done, the subshell exits and your Unix_Array ceases to exist.
In this case, the simplest fix is not to use a pipe:
counter=0
while read line; do
Unix_Array[$counter]=$line;
let counter=counter+1;
echo $counter;
done < hello.txt
echo ${Unix_Array[0]}
echo ${Unix_Array[1]}
echo ${Unix_Array[2]}
By the way, you don't really need the counter. An easier way to write this might be:
$ oIFS="$IFS" # Save the old input field separator
$ IFS=$'\n' # Set the IFS to a newline
$ some_array=($(<hello.txt)) # Splitting on newlines, assign the entire file to an array
$ echo "${some_array[2]}" # Get the third element of the array
c
$ echo "${#some_array[#]}" # Get the length of the array
4
If you are using Bash v4 or higher, you can use mapfile to accomplish this:
mapfile -t Unix_Array < hello.txt
Otherwise, this should work:
while read -r line; do
Unix_Array+=("$line")
done < hello.txt
The best way I found is:
declare -a JUPYTER_VENV
JUPYTER_VENV+=( "test1" "test2" "test3" )
And then consume it with:
for jupenv in "${JUPYTER_ENV[#]}"
do
echo "$jupenv"
done
Instead of this:
cat hello.txt | while read line; do
${Unix_Array[${counter}]}=$line;
let counter=counter+1;
echo $counter;
done
You can just do this:
Unix_Array=( `cat "hello.txt" `)
It’s a solution:
count=0
Unix_Array=($(cat hello.txt))
array_size=$(cat hello.txt | wc -l)
for ((count=0; count < array_size; count++))
do
echo ${Unix_Array[$count]}
done

Unix Shell Script - Putting $input into file

I need to put $input into the file spelled.. but this code leaves the file empty. I am only correcting the first word that comes up in the array, so i'm thinking that when I just hit 'Enter' that it is rewriting the file over? any help?
# store words in file
cat $1 | ispell -l > file
# move words in file into array
array=($(< file))
# remove temp file
rm file
# print out words & ask for corrections
for ((i=0; i<${#array[#]}; i++ ))
do
read -p "' ${array[i]} ' is mispelled. Press "Enter" to keep
this spelling, or type a correction here: " input
echo $input > spelled
done
# show corrections
cat spelled
Use >> instead of > if you want to append when the file exists.
echo $input >> spelled

Resources