I'm trying to test my homework (knapsack problem) and it's getting repetitive entering all these inputs every time I recompile.
Here's what I've got:
will#will-mint ~/code/byun-sp15 $ g++ knapsack.cpp
will#will-mint ~/code/byun-sp15 $ ./a.out
Please enter number of items: 3
Please enter Knapsack Capacity: 10
Enter weights and values of 3 items:
Item 1: 3 40
Item 2: 2 10
Item 3: 5 50
0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 *
0 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 *
0 * 50 * 50 * 99 * 50 * 50 * 60 * 60 * 60 * 60 *
Clearly my table is not correct, please do not help me there. NO SPOILERS!
I put 3 10 3 40 2 10 5 50 in test.txt and tried the following:
will#will-mint ~/code/byun-sp15 $ vim test.txt
will#will-mint ~/code/byun-sp15 $ test.txt > ./a.out
test.txt: command not found
will#will-mint ~/code/byun-sp15 $ cat test.txt | ./a.out
will#will-mint ~/code/byun-sp15 $ cat test.txt | ./a.out >> output.log
will#will-mint ~/code/byun-sp15 $ vim output.log
will#will-mint ~/code/byun-sp15 $ cat test.txt | ./a.out 2> output.log
will#will-mint ~/code/byun-sp15 $ vim output.log
will#will-mint ~/code/byun-sp15 $ ./a.out < test.txt
will#will-mint ~/code/byun-sp15 $ cat test.txt | ./a.out > output.log
will#will-mint ~/code/byun-sp15 $ vim output.log
will#will-mint ~/code/byun-sp15 $ ./a.out << test.txt
None of which worked. I need help with my bash-fu, how can I use a string of space-separated numbers in a text file as input for my a.out?
Related
I have a string consist of words, special characters (*, |, ( etc.) and numbers(floating). I want to remove white spaces between only words and special characters. Spaces between numbers should not be removed. How I can do it in Perl?
E.g.:
Rama 1 * 2.34 * ( L - 0.45 ) XYZ 10 20.05 30.06 40 P > 25.
It should be after conversion:
Rama1*2.34*(L-0.45)XYZ 10 20.05 30.06 40 P>25.
(?<!\d)\h+|\h+(?!\d)
You can use lookarounds here.See demo.
https://regex101.com/r/uF4oY4/62
You may use the below lookaround based regex.
perl -pe 's/\s+(?=\D)|(?<=\D)\s+//g' file
Example:
$ echo 'Rama 1 * 2.34 * ( L - 0.45 ) XYZ 10 20.05 30.06 40 P > 25.' | perl -pe 's/\s+(?=\D)|(?<=\D)\s+//g'
Rama1*2.34*(L-0.45)XYZ10 20.05 30.06 40P>25.
or
$ echo 'Rama 1 * 2.34 * ( L - 0.45 ) XYZ 10 20.05 30.06 40 P > 25.' | perl -pe 's/(?<=[^\s\w])\s+|\s+(?=[^\w\s])//g'
Rama 1*2.34*(L-0.45)XYZ 10 20.05 30.06 40 P>25.
I have a large number of files in this format (iPhone camera):
Photo 31-12-13 12 59 59.jpg
How can I batch rename these files using the OSX command line to this (ISO) format:
2013-12-31 12 59 59.jpg
I have tried using the command below, but it doesn't seem to work:
for i in Photo*
do
mv "$i" "`echo $i | sed 's_Photo ([0-9]+)-([0-9]+)-([0-9]+) (.*)_\3-\2-\1 \4_/'`”
done
You can use:
for i in Photo*; do
mv "$i" "$(sed -E 's/^Photo ([0-9]*)-([0-9]*)-([0-9]*) (.*)$/20\3-\2-\1 \4/' <<< "$i")"
done
You have a stray slash.
sed's basic regular expressions need lots of backslashes. Try one of
mv "$i" "$(echo "$i" | sed -r 's_Photo ([0-9]+)-([0-9]+)-([0-9]+)_\3-\2-\1_')"
mv "$i" "$(echo "$i" | sed 's_Photo \([0-9]\+\)-\([0-9]\+\)-\([0-9]\+\)_\3-\2-\1_')"
Note you don't have to capture the end of the line just to refer to it unchanged.
Also the ending double quote at the end of the line is not a plain double quote:
$ od -c <<< ' mv "$i" "`echo $i | sed '\''s_Photo ([0-9]+)-([0-9]+)-([0-9]+) (.*)_\3-\2-\1 \4_/'\''`”'
0000000 m v " $ i " " ` e c h o
0000020 $ i | s e d ' s _ P h o
0000040 t o ( [ 0 - 9 ] + ) - ( [ 0 -
0000060 9 ] + ) - ( [ 0 - 9 ] + ) ( .
0000100 * ) _ \ 3 - \ 2 - \ 1 \ 4 _ /
0000120 ' ` 342 200 235 \n
0000126
I am trying to count the number of matched terms from an input list containing one term per line with a data file and create an output file containing both the matched (grep'd) term with the number of matched terms and where there isn't match, to return a value of zero.
Input list:
+ 5S_rRNA
+ 7SK
+ AC001
+ AC000111.3
+ AC000111.6
The data.txt file:
chr10 101780038 101780209 5S_rRNA
chr10 103578280 103578430 5S_rRNA
chr10 112327234 112327297 5S_rRNA
chr10 120766459 120766601 7SK
chr10 127408228 127408317 7SK
chr10 127511874 127512063 AADAC
chr10 14614140 14614294 AC000111.3
I would like to create an output file containing all the unmatched terms and matched terms with the corresponding count to look like this:
+ 5S_rRNA 3
+ 7SK 2
+ AC001 0
+ AADAC 1
+ AC000111.3 1
+ AC000111.6 0
I can create an output file containing matched terms and the counts but I don't know how to get the zero value to be returned if there isn't a match and get it to print all the output to a separate file.
These are the codes I have used to create matched terms (thanks perreal and Mark Setchell)
#!/bin/bash
while read line
do
line=${line##+ } # Strip off leading + and space
n=$(grep "$line" data.txt 2> /dev/null | wc -l)
if [ $n -gt 0 ]; then
echo $line
echo $n
fi
done < input_list.txt > output.txt
and
cut -d' ' -f2 input.txt | grep -o -f - data.txt | sort | uniq -c | \
sed 's/\s*\([0-9]*\)\s*\(.*\)/+ \2\t\1/' > output.txt
Any suggestions would be great. Thanks
Harriet
You can use this simple loop with grep -c:
while read l; do echo -n "+ $l "; grep -c "$l" file1; done < inputs
+ 5S_rRNA 3
+ 7SK 2
+ AC001 0
+ AC000111.3 1
+ AC000111.6 0
cut -d' ' -f2 input.txt | grep -o -f - data.txt | sort | uniq -c | \
sed 's/\s*\([0-9]*\)\s*\(.*\)/+ \2 \1/' | \
join -a 1 -e 0 -j 2 input.txt - -o '1.2 2.3' | \
sed 's/ /\t/;s/^/+ /'
When working with tab, whitespace or similar delimited files, think awk. Perhaps this is what you're looking for. I have used a ternary operator, but you could use if / else statements if you find them easier to read.
awk 'FNR==NR { a[$4]++; next } { print "+", $2, $2 in a ? a[$2] : 0 }' data.txt inputlist.txt
Results:
+ 5S_rRNA 3
+ 7SK 2
+ AC001 0
+ AC000111.3 1
+ AC000111.6 0
$2 in a ? a[$2] : 0 means if column two is in the array (called a), return the value for that key. Else, return zero. HTH.
I have a text file in the following format:
variableStep chrom=chr1 span=10
10161 1
10171 1
10181 2
10191 2
10201 2
10211 2
10221 2
10231 2
10241 2
10251 1
variableStep chrom=chr10 span=10
70711 1
70721 2
70731 2
70741 2
70751 2
70761 2
70771 2
70781 2
70791 1
71161 1
71171 1
71181 1
variableStep chrom=chr11 span=10
104731 1
104741 1
104751 1
104761 1
104771 1
104781 1
104791 1
104801 1
128711 1
128721 1
128731 1
I need a way to break this down into several files named for example "chr1.txt", "chr10.txt and "chr11.txt". How would I go about doing this?
I about the the following way:
cat file.txt | \
while IFS=$'\t' read -r -a rowArray; do
echo -e "${rowArray[0]}\t${rowArray[1]}\t${rowArray[2]}"
done > $file.mod.txt
That reads line by line and then saves line by line. However, I need something a little more elaborate that spans rows. "chr1.txt" would include everything from the row 10161 1 to row 10251 1, "chr10.txt" would include everything from the row 70711 1 to row 71181 1, etc. It's also specific in that I have to read in the actual chr# from each line as well, and save that as the file name.
The help is really appreciated.
awk -F'[ =]' '
$1 == "variableStep" {file = $3 ".txt"; next}
file != "" {print > file}' < input.txt
This worked for me:
IFS=$'\n'
curfile=""
content=($(< file.txt))
for ((idx = 0; idx < ${#content[#]}; idx++)); do
if [[ ${content[idx]} =~ ^.*chrom=(\\b.*?\\b)\ .*$ ]]; then
curfile="${BASH_REMATCH[1]}.txt"
rm -rf ${curfile}
elif [ -n "${curfile}" ]; then
echo ${content[idx]} >> ${curfile}
fi
done
Awk is appropriate for this problem domain because the text file is already (more or less) organized into columns. Here's what I would use:
awk 'NF == 3 && index($2, "=") { filename = substr($2, index($2, "=") + 1) }
NF == 2 && filename { print $0 > (filename ".txt") }' < input.txt
Explanation:
Think of the lines starting with variableStep as "three columns" and the other lines as "two columns". The above script says, "Parse the text file line-by-line; if a line has three columns and the second column contains an '=' character, assign 'all of the characters in the second column that occur after the '=' character' to a variable called filename. If a line has two columns and the filename variable's been assigned, write the entire line to the file that's constructed by concatenating the string in the filename variable with '.txt'".
Notes:
NF is a built-in variable in Awk that represents the "number of fields", where a "field" (in this case) can be thought of as a column of data.
$0 and $2 are built-in variables that represent the entire line and the second column of data, respectively. ($1 represents the first column, $3 represents the third column, etc...)
substr and index are built-in functions described here: http://www.gnu.org/software/gawk/manual/gawk.html#String-Functions
The redirection operator (>) acts differently in Awk than it does in a shell script; subsequent writes to the same file are appended.
String concatenation is performed simply by writing expressions next to each other. The parenthesis ensure the concatenation happens before the file gets written to.
More details can be found here: http://www.gnu.org/software/gawk/manual/gawk.html#Two-Rules
i used sed to filter ....
code part :
Kaizen ~/so_test $ cat zsplit.sh
cntr=1;
prev=1;
for curr in `cat ztmpfile2.txt | nl | grep variableStep | tr -s " " | cut -d" " -f2 | sed -n 's/variableStep//p'`
do
sed -n "$prev,$(( ${curr} - 1))p" ztmpfile2.txt > zchap$cntr.txt ;
#echo "displaying : : zchap$cntr.txt " ;
#cat zchap$cntr.txt ;
prev=$curr; cntr=$(( $cntr + 1 ));
done
sed -n "$prev,$ p" ztmpfile2.txt > zchap$cntr.txt ;
#echo "displaying : : zchap$cntr.txt " ;
#cat zchap$cntr.txt ;
output :
Kaizen ~/so_test $ ./zsplit.sh
+ ./zsplit.sh
zchap1.txt :: 1 :: 1
displaying : : zchap1.txt
variableStep chrom=chr1 span=10
zchap2.txt :: 1 :: 12
displaying : : zchap2.txt
variableStep chrom=chr1 span=10
10161 1
10171 1
10181 2
10191 2
10201 2
10211 2
10221 2
10231 2
10241 2
10251 1
zchap3.txt :: 12 :: 25
displaying : : zchap3.txt
variableStep chrom=chr10 span=10
70711 1
70721 2
70731 2
70741 2
70751 2
70761 2
70771 2
70781 2
70791 1
71161 1
71171 1
71181 1
displaying : : zchap4.txt
variableStep chrom=chr11 span=10
104731 1
104741 1
104751 1
104761 1
104771 1
104781 1
104791 1
104801 1
128711 1
128721 1
128731 1
from the result zchap* files , iff you want you can remove the line : variableStep chrom=chr11 span=10 by using sed -- sed -i '/variableStep/d' zchap*
does this help ?
My script
#!/bin/bash
echo -n "number 1 :";
read number1
echo -n "number 2 :";
read number2
jlh=$(echo $number1 + $number2 | bc -l | sed 's/^\./0./');
echo "your result : $number1 + $number2 = $jlh "
if input for number 1 is -1 , and number 2 is 0.9, why the result only -.1.
I want to show the zero like this.
Your result : -1 + 0.9 = -0.1
How I can do it?
Because you by now just consider the case .NNN, but not the -.NNN, that is having the minus - sign before:
With this it should work:
sed -e 's/^\./0./' -e 's/^-\./-0./'
start with . start with -.
All together;
jlh=$(echo $number1 + $number2 | bc -l | sed -e 's/^\./0./' -e 's/^-\./-0./');