I have an file like this:
EMP Ename Sal Comm
101 Ravi $800 500
102 Ram $1000 40
103 Shyam $#400 50
I want to convert it to this form:
EMP Ename Sal Comm
101 Ravi 800 500
102 Ram 1000 40
103 Shyam 400 50
The file is tab delimited. I've tried with awk, but unfortunately I couldn't remove the "$#" part from the Sal column.
This should do the trick:
sed -e 's/\$//g' -e 's/\#//g' file > newFile
Slightly confused about what you mean about not needing it permanently.
Alternatively, in vi:
:%s/\$//g'
:%s/\#//g'
If you want to modify a file inside a vi session
Related
I have a set of text files in a folder, like so:
a.txt
1
2
3
4
5
b.txt
1000
1001
1002
1003
1004
.. and so on (assume fixed number of rows, but unknown number of text files). What I am looking a results file which is a summation across all rows:
result.txt
1001
1003
1005
1007
1009
How do I go about achieving this in bash? without using Python etc.
Using awk
Try:
$ awk '{a[FNR]+=$0} END{for(i=1;i<=FNR;i++)print a[i]}' *.txt
1001
1003
1005
1007
1009
How it works:
a[FNR]+=$0
For every line read, we add the value of that line, $0, to partial sum, a[FNR], where a is an array and FNR is the line number in the current file.
END{for(i=1;i<=FNR;i++)print a[i]}
After all the files have been read in, this prints out the sum for each line number.
Using paste and bc
$ paste -d+ *.txt | bc
1001
1003
1005
1007
1009
I want to store row results of a query in array in unix scripting.
I tried this :
array=`sqlplus -s $DB <<eof
select customer_id from customer;
eof`;
When i tried to print it , it shows me this result:
echo ${array[0]};
CUSTOMER_ID ----------- 1 2 51 52 101 102 103 104 105 106 108 11 rows selected.
But I want to store each row as an element by excluding column_name and that "11 rows selected" sentence.
Thanks in Advance.
To create an array you need this syntax in BASH:
array=($(command))
or:
declare -a array=($(command))
For your sqlplus command:
array=($(sqlplus -s "$DB"<<eof
SET PAGESIZE 0;
select customer_id from customer;
eof))
and then print it as:
printf "%\n" "${array[#]}"
Just note that it is subject to all the shell expansions on white-spaces.
This question may be too simple for most of you, but I really don't want to do it manually. So, suppose I have a file in terminal such like this:
[dz2t#edison-s GWAS]$ head $PHENO
GWASID CHILDID VSTNUM GENDER GA
1001 1001 11 Female -9
1002 1002 11 Male -9
1003 1003 11 male -9
1004 1004 11 male -9
1005 1005 11 Female -9
1006 1006 11 Female -9
How to change the names of first two columns from "GWASID" and "CHILDID" to "FID" and "IID". And the others remain unchanged?
You can do this:
sed -e '1s/GWASID/FID/' -e '1s/CHILDID/IID/' YourFile
That says, on line 1 only, substitute GWASID with FID and CHILDID with IID. If you want it to overwrite the file, you need to add the -i flag after the word sed for in-place editing.
I'm developing a shell script but I am stuck with the below part.
I have the file sample.txt:
S.No Sub1 Sub2
1 100 200
2 100 200
3 100 200
4 100 200
5 100 200
6 100 200
7 100 200
I want to search the S.No column in sample.txt. For example if I'm searching the value 5 I need the rows up to 5 only I don't want the rows after the value of in S.NO is larger than 5.
the output must look like, output.txt
S.No Sub1 Sub2
1 100 200
2 100 200
3 100 200
4 100 200
5 100 200
Print the first line and any other line where the first field is less than or equal to 5:
$ awk 'NR==1||$1<=5' file
S.No Sub1 Sub2
1 100 200
2 100 200
3 100 200
4 100 200
5 100 200
Using perl:
perl -ane 'print if $F[$1]<=5' file
And the sed solution
n=5
sed "/^$n[[:space:]]/q" filename
The sed q command exits after printing the current line
The suggested awk relies on that column 1 is numeric sorted. A generic awk that fulfills the question title would be:
gawk -v p=5 '$1==p {print; exit} {print}'
However, in this situation, sed is better IMO. Use -i to modify the input file.
sed '6q' sample.txt > output.txt
Suppose I have two tab-delimited files that share a column. Both files have a header line that gives a label to each column. What's an easy way to take the union of the two tables, i.e. take the columns from A and B, but do so according to the value of column K?
for example, table A might be:
employee_id name
123 john
124 mary
and table B might be:
employee_id age
124 18
123 22
then the union based on column 1 of table A ("employee_id") should yield the table:
employee_id name age
123 john 22
124 mary 18
i'd like to do this using Unix utilities, like "cut" etc. how can this be done?
you can use the join utility, but your files need to be sorted first.
join file1 file2
man join for more information
here's a start. I leave you to format the headers as needed
$ awk 'NR>1{a[$1]=a[$1]" "$2}END{for(i in a)print a[i],i}' tableA.txt tableB.txt
age employee_id
john 22 123
mary 18 124
another way
$ join <(sort tableA.txt) <(sort tableB.txt)
123 john 22
124 mary 18
employee_id name age
experiment with the join options when needed (see info page or man page)
Try:
paste file1 file2 > file3