dialog --menu selected option - linux

My script is:
dialog --title 'Example' --menu 'Select:' 0 0 0 1 'ABC' 2 'DEF' 3 'GHI'
I would like that option 2 was selected by default (over). Its possible?

Try:
dialog --title 'Example' --default-item '2' --menu 'Select:' 0 0 0 1 'ABC' 2 'DEF' 3 'GHI'

dialog --title "Example" --default-item '2' --menu "Select:" 25 25 3 1 "ABC" 2 "DEF" 3 "GHI"
use of ["] for strings is most recommended
--default-item is common option provided into dialog utilities for menu box or forms.
This will keep selection on '2' option.

Related

Recode column values in unix

1859115 2258379 24636 Yes 06S14028968 13 1 1 2
1859115 2258379 24636 Yes 06S14028968 13 1 1 2
1859116 2255037 21608 Yes 06S14028969 11 0 2 3
1859117 2268746 34027 Yes 06S14028970 10 0 2 1
Above is the example of my data set. I want to replace the values of 7th column in a way that 1 should be replaced by 2 and 0 should be replaced by 1. So the outcome i am expecting should be like following.
1859115 2258379 24636 Yes 06S14028968 13 2 1 2
1859115 2258379 24636 Yes 06S14028968 13 2 1 2
1859116 2255037 21608 Yes 06S14028969 11 1 2 3
1859117 2268746 34027 Yes 06S14028970 10 1 2 1
I have tried using this approach
awk 'NR==1{$10="Pheno";print;next}\
$7 == "1" {$10="2"};\
$7 == "0" {$10="1"}1' old.txt |column -t > new.txt
and then removing the first row and extracting columns of interest. But i need straight forward way.
This could be simply done by putting a simple condition to check if NF(number of fields in each line) is greater OR equal to 7 then increment 7th field with 1 and print edited/non-edited line then(by doing this we can avoid adding 1 if number of fields are lesser than 7 in any line).
awk 'NF>=7{$7+=1} 1' Input_file

How to replace a number to another number in a specific column using awk

This is probably basic but I am completely new to command-line and using awk.
I have a file like this:
1 RQ22067-0 -9
2 RQ34365-4 1
3 RQ34616-4 1
4 RQ34720-1 0
5 RQ14799-8 0
6 RQ14754-1 0
7 RQ22101-7 0
8 RQ22073-1 0
9 RQ30201-1 0
I want the 0s to change to 1 in column3. And any occurence of 1 and 2 to change to 2 in column3. So essentially only changing numbers in column 3. But I am not changing the -9.
1 RQ22067-0 -9
2 RQ34365-4 2
3 RQ34616-4 2
4 RQ34720-1 1
5 RQ14799-8 1
6 RQ14754-1 1
7 RQ22101-7 1
8 RQ22073-1 1
9 RQ30201-1 1
I have tried using (see below) but it has not worked
>> awk '{gsub("0","1",$3)}1' PRS_with_minus9.pheno.txt > PRS_with_minus9_modified.pheno
>> awk '{gsub("1","2",$3)}1' PRS_with_minus9.pheno.txt > PRS_with_minus9_modified.pheno
Thank you.
With this code in your question:
awk '{gsub("0","1",$3)}1' PRS_with_minus9.pheno.txt > PRS_with_minus9_modified.pheno
awk '{gsub("1","2",$3)}1' PRS_with_minus9.pheno.txt > PRS_with_minus9_modified.pheno
you're running both commands on the same input file and writing their
output to the same output file so only the output of the 2nd script
will be present in the output, and
you're trying to change 0 to 1
first and THEN change 1 to 2 so the $3s that start out as 0 would
end up as 2, you need to change the order of the operations.
This is what you should be doing, using your existing code:
awk '{gsub("1","2",$3); gsub("0","1",$3)}1' PRS_with_minus9.pheno.txt > PRS_with_minus9_modified.pheno
For example:
$ awk '{gsub("1","2",$3); gsub("0","1",$3)}1' file
1 RQ22067-0 -9
2 RQ34365-4 2
3 RQ34616-4 2
4 RQ34720-1 1
5 RQ14799-8 1
6 RQ14754-1 1
7 RQ22101-7 1
8 RQ22073-1 1
9 RQ30201-1 1
The gsub() should also just be sub()s as you only want to perform each substitution once, and you don't need to enclose the numbers in quotes so you could just do:
awk '{sub(1,2,$3); sub(0,1,$3)}1' file
You can check the value of column 3 and then update the field value.
Check for 1 as the first rule because if the first check is for 0, the value will be set to 1 and the next check will set the value to 2 resulting in all 2's.
awk '
{
if($3==1) $3 = 2
if($3==0) $3 = 1
}
1' file
Output
1 RQ22067-0 -9
2 RQ34365-4 2
3 RQ34616-4 2
4 RQ34720-1 1
5 RQ14799-8 1
6 RQ14754-1 1
7 RQ22101-7 1
8 RQ22073-1 1
9 RQ30201-1 1
With your shown samples and ternary operators try following code. Simple explanation would be, checking condition if 3rd field is 1 then set it to 2 else check if its 0 then set it to 0 else keep it as it is, finally print the line.
awk '{$3=$3==1?2:($3==0?1:$3)} 1' Input_file
Generic solution: Adding a Generic solution here, where we can have 3 awk variables named: fieldNumber in which you could mention all field numbers which we want to check for. 2nd one is: existValue which we want to match(in condition) and 3rd one is: newValue new value which needs to be there after replacement.
awk -v fieldNumber="3" -v existValue="1,0" -v newValue="2,1" '
BEGIN{
num=split(fieldNumber,arr1,",")
num1=split(existValue,arr2,",")
num2=split(newValue,arr3,",")
for(i=1;i<=num1;i++){
value[arr2[i]]=arr3[i]
}
}
{
for(i=1;i<=num;i++){
if($arr1[i] in value){
$arr1[i]=value[$arr1[i]]
}
}
}
1
' Input_file
This might work for you (GNU sed):
sed -E 's/\S+/\n&\n/3;h;y/01/12/;G;s/.*\n(.*)\n.*\n(.*)\n.*\n.*/\2\1/' file
Surround 3rd column by newlines.
Make a copy.
Replace all 0's by 1's and all 1's by 2's.
Append the original.
Pattern match on newlines and replace the 3rd column in the original by the 3rd column in the amended line.
Also with awk:
awk 'NR > 1 {s=$3;sub(/1/,"2",s);sub(/0/,"1",s);$3=s} 1' file
1 RQ22067-0 -9
2 RQ34365-4 2
3 RQ34616-4 2
4 RQ34720-1 1
5 RQ14799-8 1
6 RQ14754-1 1
7 RQ22101-7 1
8 RQ22073-1 1
9 RQ30201-1 1
the substitutions are made with sub() on a copy of $3 and then the copy with the changes is assigned to $3.
When you don't like the simple
sed 's/1$/2/; s/0$/1/' file
you might want to play with
sed -E 's/(.*)([01])$/echo "\1$((\2+1))"/e' file

Inserting multiple files into a template

I have a template file and need to insert data from multiple files into this file. The template (template.txt) is laid out like so:
Title
Data 1
Data 2
Data 3
I need to put each data set under its title. So say the data files are:
Data1.dat Data2.dat Data3.dat
1 2 3 0 0 0 500 300 100
4 5 6 0 0 0 400 200 000
The final product needs to be:
Title
Data 1
1 2 3
4 5 6
Data 2
0 0 0
0 0 0
Data 3
500 300 100
400 200 000
How can I make this possible? I can insert one data set into the template using:
sed '/Data 1/r Data1.dat' template.txt
I want to be able to do it for as many data files as needed and can't figure out how to automate it.
this would do...
while read -r line;
do
file=$(echo $line | sed 's/ //;s/$/.dat/');
echo $line;
if [ -f "$file" ]; then cat "$file"; fi;
done < template.txt
There's a lot of details left out of your post that could affect this, but given what you've asked, this seems to work assuming each line is the exact filename with spaces removed and missing the .dat extension, and that all files exist.
$ cat template.txt
Title
Data 1
Data 2
Data 3
$ awk 'NR==1{print;next;}{print;filename=$0;gsub(" ","",filename);system("cat "filename".dat");}' template.txt
Title
Data 1
1 2 3
4 5 6
Data 2
0 0 0
0 0 0
Data 3
500 300 100
400 200 000
NR==1{print;next;} emits the first line and then goes to the next.
{print;filename=$0".dat";gsub(" ","",filename);system("cat "filename");}
for all other lines, print the line, assign it to a var and append .dat, then replace spaces and make a system call to cat that file.
Another example:
$ awk 'NR==1{print;next;}{print;filename=$0".dat";gsub(" ","",filename);system("cat "filename);}' template.txt
Title
Data 1
1 2 3
4 5 6
Data 2
0 0 0
0 0 0
Data 3
500 300 100
400 200 000
Data 4
4-1 4-2 4-3
4-1 4-2 4-3
4-1 4-2 4-3
Data 5
5-1 5-2 5-3
5-1 5-2 5-3
5-1 5-2 5-3
Data 6
6-1 6-2 6-3
6-1 6-2 6-3
6-1 6-2 6-3
Data 7
7-1 7-2 7-3
7-1 7-2 7-3
7-1 7-2 7-3
$ cat tst.awk
NR==FNR {
if ( FNR==1 ) {
print
}
else {
filename = $0 ".dat"
gsub(/[[:space:]]/,"",filename)
title[filename] = $0
ARGV[ARGC] = filename
ARGC++
}
next
}
FNR==1 { print title[FILENAME] }
{ print }
$ awk -f tst.awk template.txt
Title
Data 1
1 2 3
4 5 6
Data 2
0 0 0
0 0 0
Data 3
500 300 100
400 200 000

How can I separate some repeated patterns in a row into multiple rows using bash script?

I have some problem with bash script.
I've got a string which has some repeated patterns like this.
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 ...
Each fields is separated by tab key.
I want it to look like this...
1 2 3 4
1 2 3 4
1 2 3 4
…
How can I solve this problem using bash script like cut, sed, awk ... ?
I've tried some command like cut -f 'seq 4, 4, 40' example.txt
It doesn't work...
It looks very easy but so difficult to me...
You can use sed like this:
s='1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4'
p='1 2 3 4'
echo "$s"|sed "s/$p\s*/&\n/g"
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Live Demo: http://ideone.com/P59OCJ
Here's a pure bash solution:
IFS=$'\t' set -- $(<input_file)
seen=()
while [[ $1 ]]; do
if (( ${seen[$1]} )); then # If we've seen the value before, start a new line.
echo
unset seen
fi
printf '%s ' "$1"
seen[$1]=1
shift
done
If you know the ending number of your sequence beforehand, you can do something like:
LAST_NUMBER=4
sed -e "s/$LAST_NUMBER\t*/&\n/g" < example.txt
Just replace 4 with the last number from the sequence
If you don't know the number, you have to search through it using the following:
#!/bin/bash
declare -A CHECKED_NUMBERS
LAST_NUMBER=
while read LINE; do
SPLIT_LINE=$(cut -d" " -f1- <<< "$LINE")
for number in $SPLIT_LINE; do
if [ "${CHECKED_NUMBERS[$number]}" == "1" ]; then
LAST_NUMBER=$number
else
CHECKED_NUMBERS[$number]=1
fi
done
done < example.txt
# do the replacement
sed -e "s/$LAST_NUMBER\t*/&\n/g" < example.txt
An awk version
awk '{for (i=1;i<=NF;i++) {printf "%s"(i%4?" ":"\n"),$i}}' file
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
An gnu awk version
awk -v RS="\t" '{printf "%s"(NR%4?" ":"\n"),$0}' file
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
xargs may help:
kent$ echo "1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4"|xargs -n4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
This might work for you:
printf "%s\t%s\t%s\t%s\n" $string
or you want the fields space separated:
printf "%s %s %s %s\n" $string

How to get output like SQLPLUS while Running SQL Query in shell script

I have a following shell script
RETVAL=`sqlplus -silent user/password <<EOF
SET PAGESIZE 9990
SELECT id, type, count(*) "count" FROM event
EXIT;
EOF`
echo $RETVAL
it output like
ID TYPE count ------------- ---------- ----------- 2 11 2 1 4 1 2 10 29 1 1 35 2 1 6 2 18 1 2 2 3 7 rows selected
But i want output like
ID TYPE count
------------- ---------- -----------
2 11 2
1 4 1
2 10 29
1 1 35
2 1 6
2 18 1
2 2 3
7 rows selected.
I tried to figure out if i get some new line character but couldnt find it.
Regards,
Your variable contains the newlines, but the way you're displaying it removes them.
Replace the echo statement with:
echo "$RETVAL"
The shell won't mess with the newlines then. You should pretty much always quote variables that can contain any form of whitespace that needs to be preserved.

Resources