awk simple commands merge


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk simple commands merge
# 1  
Old 03-22-2010
awk simple commands merge

Is there nice awk code for merging the following commands and do the last task?


input1

Code:
ab100    xxx    100    blahblah    +    1000
ab100    yyy    90    blahblah    +    1000
ef390    ggg    200    blahblah    -    2000
ef390    aaa    100    blahblah    -    2000
df888    ttt    300    blahhhha    -    3000
df888    bbb    300    blahhhha    -    3000

Code:
awk '{print $1,$2,$3,$4,$5,$5$6}' input1 >>input2

input2

Code:
ab100    xxx    100    blahblah    +1000
ab100    yyy    90    blahblah    +1000
ef390    ggg    200    blahblah    -2000
ef390    aaa    100    blahblah    -2000
df888    ttt    300    blahhhha    -3000
df888    bbb    300    blahhhha    -3000

Code:
awk '{print $1,$2,$3,$4,$3-$5}' input2 >>input3

input3

Code:
ab100    xxx    100    blahblah    1100
ab100    yyy    90    blahblah    1090
ef390    ggg    200    blahblah    -1800
ef390    aaa    100    blahblah    -1900
df888    ttt    300    blahhhha    -2700
df888    bbb    300    blahhhha    -2700

Code:
awk '{print $1,$2,$3,$4,abs($5)}' input3 >>input4

input4
Code:
ab100    xxx    100    blahblah    1100
ab100    yyy    90    blahblah    1090
ef390    ggg    200    blahblah    1800
ef390    aaa    100    blahblah    1900
df888    ttt    300    blahhhha    2700
df888    bbb    300    blahhhha    2700

Code:
print keys with less value in 5th column (ex: ab100 has 1090<1100)

Code:
ab100    yyy    90    blahblah    1090
ef390    ggg    200    blahblah    1800
df888    ttt    300    blahhhha    2700

# 2  
Old 03-22-2010
Try this which combines all the operations and check for $5 < 2000

Code:
awk ' { OFS="\t";$5=int($5$6)+$3; $5=($5>=0)?$5: 0-$5 ;$NF=""; } $5<2000' file

Oops.Noticed that I understood it wrongly. Anyway, Franklin has provided the solution Smilie

Last edited by dennis.jacob; 03-22-2010 at 09:22 AM..
# 3  
Old 03-22-2010
Quote:
Originally Posted by thegeek
here is a list of examples and explanations: Awk Introduction Tutorial - 7 Awk Print Examples (link removed)

I don't think he has an issue with the print command but more with the approach.

Assuming your source file has the format as input1:
Code:
awk '{s2=$5=="+"?$3+$6:$3-$6; s2*=s2<0?-1:1}
a[$1]{
  if(s1<=s2){
    print a[$1], s1
  }
  else {
    print $1,$2,$3,$4, s2
    next
  }
}
{a[$1]=$1 OFS $2 OFS $3 OFS $4; s1=s2}' OFS="\t" file

This is the result:
Code:
$ cat file
ab100    xxx    100    blahblah    +    1000
ab100    yyy    90    blahblah    +    1000
ef390    ggg    200    blahblah    -    2000
ef390    aaa    100    blahblah    -    2000
df888    ttt    300    blahhhha    -    3000
df888    bbb    300    blahhhha    -    3000
$
$ awk '{s2=$5=="+"?$3+$6:$3-$6; s2*=s2<0?-1:1}
a[$1]{
  if(s1<s2){
    print a[$1], s1
  }
  else {
    print $1,$2,$3,$4, s2
    next
  }
}
{a[$1]=$1 OFS $2 OFS $3 OFS $4; s1=s2}' OFS="\t" file
ab100	yyy	90	blahblah	1090
ef390	ggg	200	blahblah	1800
df888	ttt	300	blahhhha	2700
$

# 4  
Old 03-23-2010
hey thanx Frank

Thanx for the nice code Franklin. but it doesn't meet my requirements. may be I didn't explain clear enough. and the output has to display full values instead of 1.1454e+08 or some thing.

input

Code:
ab100    xxx    100    blahblah    +    114436183.5
ab100    xxx    100    blahblah    +    114540291.5
ab100    xxx    100    blahblah    +    114543195.5
ab100    yyy    90    blahblah    +    1000
ab100    yyy    90    blahblah    +    3000
ab100    yyy    90    blahblah    +    4000
ef390    ggg    200    blahblah    -    2000
ef390    aaa    100    blahblah    -    2000
df888    ttt    300    blahhhha    -    3000
df888    bbb    300    blahhhha    -    3000

output

Code:
ab100   xxx     100     blahblah        1.14436e+08
ab100   xxx     100     blahblah        1.1454e+08
ab100   yyy     90      blahblah        1090
ab100   yyy     90      blahblah        3090
ab100   yyy     90      blahblah        4090
ef390   ggg     200     blahblah        1800
df888   ttt     300     blahhhha        2700

The output I need should be

Code:
ab100   yyy     90      blahblah        1090
ef390   ggg     200     blahblah        1800
df888   ttt     300     blahhhha        2700

the key ab100 has low value 1090 compared all other values for the same key.
# 5  
Old 03-23-2010
The first solution assumes consecutive pairs of lines, try this one:
Code:
awk '{s=$5=="+"?$3+$6:$3-$6; s*=s<0?-1:1}
a[$1] && s>a[$1]{next}
{a[$1]=s; b[$1]=$1 OFS $2 OFS $3 OFS $4}
END{
  for(i in b){printf("%s\t%.0f\n", b[i], a[i])}
}' OFS="\t" file

# 6  
Old 03-23-2010
thank you very much

All the codes wrking fine. Thank you very much. So helpfulbv
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge output of 2 commands into variable

I am extracting two pieces of information from the following file: /proc/cpuinfo, that I need to merge into one report. The first command: grep -i processor /proc/cpuinfo | awk '{print $1$2,$3}' yields: processor: 0 processor: 1 processor: 2 processor: 3 The second command: grep -i... (4 Replies)
Discussion started by: jamarsh
4 Replies

2. Homework & Coursework Questions

Need help with simple Linux commands

Hey guys, I need help with simple unix commands. I'm a newbie to Unix and don't know these commands. Any help is appreciated. 1. Logon to Linux. 2. Create a directory "Unix" under your home directory. Command(s): …………………………………………. 3. Create four... (1 Reply)
Discussion started by: loverangerguy
1 Replies

3. UNIX for Dummies Questions & Answers

Need help with simple Linux commands - NEWBIE here!

Hey guys, I need help with simple unix commands. I'm a newbie to Unix and don't know these commands. Any help is appreciated. 1. Logon to Linux. 2. Create a directory "Unix" under your home directory. Command(s): …………………………………………. 3. Create four... (1 Reply)
Discussion started by: loverangerguy
1 Replies

4. UNIX for Advanced & Expert Users

How to merge two commands

I have input like Unload: 2610000 225 2198 374 315 420 1149 57 2611 595 662 374 820 130 2938 486 2483 397 760 using these values, i need to divide first number with second number, means 225/2198, and using the value i'm trying to sort it. After sort i need first and "Unload" values,... (2 Replies)
Discussion started by: arivu198314
2 Replies

5. UNIX for Dummies Questions & Answers

help with simple terminal commands

i am at home with a windows xp home, and i am using putty terminal to access my linux mathlab account, my task is to compile and run a C program, called a.c, i used gcc -Wall -g -o mycode a.c to compile it into a mycode file now when i want to run it, i was told i had to use $... (2 Replies)
Discussion started by: omega666
2 Replies

6. Shell Programming and Scripting

Merge all commands into one

how can i merge follwoing process to one... tail +5 rawdata_AAA_1.txt_$$ | grep -v "^$" >> rawdata_AAA_2.txt_$$ For discarding first 5 rows and deleting null rows awk '!/^ /{if(a) print a; a=$0}/^ /{print a}' rawdata_AAA_2.txt >> rawdata_AAA_3.txt For merging record if it split into 2 rows... (8 Replies)
Discussion started by: Amit.Sagpariya
8 Replies

7. Red Hat

Writing simple python setup commands

Building software in most languages is a pain. Remember ant build.xml, maven2 pom files, and multi-level makefiles? Python has a simple solution for building modules, applications, and extensions called distutils. Disutils comes as part of the Python distribution so there are no other packages... (0 Replies)
Discussion started by: Linux Bot
0 Replies

8. Shell Programming and Scripting

Simple rm commands in my server

Hi all, I have PLESK to manage my virtual dedicated server. The most recent version left a favicon.ico file in all my domains and subdomains. I want to delete them without having to go into each individual folder. So I'd like to remove favicon.ico from every subfolder of /var/www/vhosts/ ... (4 Replies)
Discussion started by: chickenhouse
4 Replies

9. UNIX for Dummies Questions & Answers

simple Unix commands

Just hoping someone can help me out. I am looking for what should be simple commands to enter for this information: List of all Unix users (is this etc/passwd?) List of all users' access capabilities (is this etc/group?) Password settings (e.g., password expiration interval, minimum password... (2 Replies)
Discussion started by: i don't care
2 Replies

10. UNIX for Dummies Questions & Answers

simple commands

Can anybody help me how to display a list of unix commands. I have an account in unix,just start to use it, but don't know how (1 Reply)
Discussion started by: aningsabah
1 Replies
Login or Register to Ask a Question