awk - How to join the string with values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk - How to join the string with values
# 1  
Old 05-13-2013
awk - How to join the string with values

Hi, I'm a beginner in awk script. I've been trying to figure how to concatenate two string in input file using awk after the value is calculated.

I'm trying to get this format
Code:
Apple 5.2(10) Orange 4.4(8)
Watermelon 3.10(30) Berries 10.2(20)

The input file with the format
fruit amount.price

I've done the calculation and i manage to concatenate two string(without the total value) and the output is
Code:
Apple 5.2 Orange 4.4
Watermelon 3.10 Berries 10.2

When I try to insert the total value, the output is messed up.
# 2  
Old 05-13-2013
could you please post your code? We can try to correct it..
# 3  
Old 05-14-2013
This is what I've gotten so far
Code:
  amount = substr($NF, 1, 2)
  total_amount = amount * 2

  if( total_amount <= 18 )
  {
	price = substr($NF,3)
  }
  else
  {
	price = substr($NF, 4)
  }

  total = total_amount * price
  getline a

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. UNIX for Dummies Questions & Answers

Join On A String

Is is possible to join on unsorted files by a string? Don't want to sort because there is other text that is already in a good format. File1 has this text string: ABCD-123 FGH File2 has this text string: ABCD-123 I want to replace or join ABCD-123 in file 2 with ABCD-123 FGH in file... (11 Replies)
Discussion started by: jimmyf
11 Replies

3. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

4. Shell Programming and Scripting

join rows based on the column values

Hi, Please help me to convert the input file to a new one. input file: -------- 1231231231 3 A 4561223343 0 D 1231231231 1 A 1231231231 2 A 1231231231 4 D 7654343444 2 A 4561223343 1 D 4561223343 2 D the output should be: -------------------- 1231231231 3#1#2 A 4561223343 0 D... (3 Replies)
Discussion started by: vsachan
3 Replies

5. Shell Programming and Scripting

Join lines with the same start string

I have the text like: DN11-001 Thats the first line which needs to be DN11-001 joined with the second line and also to DN11-001 the third line as they all begin with the same DN11-001 document number. DN11-002 The number of lines differ DN11-002 among the documents. DN11-005 It can also be... (10 Replies)
Discussion started by: andrejm
10 Replies

6. Shell Programming and Scripting

Join two files values duplciating first file

Hi all, I am tryin to join two files, duplicating the alues from the first one as many times as the second. File one : 11 211 File two: a b c Result: 11 211 a 11 211 b 11 211 c Any advise will be apreciate. Thanks (3 Replies)
Discussion started by: valigula
3 Replies

7. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

8. Shell Programming and Scripting

[ksh, awk] determine values from rather complcated string

Hi there, I tried and tried, but cannot solve this myself. I have this string: enterprises.alcatel.nmu.genos.alarmHandoff.alarmHandoffPurge.purgelistAlarmIds.0 = {alarmId=63868|friendlyName=Dortmund/r01sr1sl01/port#06-#01-Vc12|probableCause=Server Signal... (3 Replies)
Discussion started by: ejdv
3 Replies

9. Shell Programming and Scripting

Join values

Guys i have joined columns of 2 files based on same key word by using sort and join commands But struggling with a another type of joining required to read multiple specific columns Could u plz help me on this:confused::confused: input 1 ax0 \tab\ aF0 \tab\ ax2 \tab\ aF2 input2 ... (3 Replies)
Discussion started by: repinementer
3 Replies

10. Shell Programming and Scripting

Use of uninitialized value in join or string at arraydef.pl

When try to execute the following script, its throwing this error: " Use of uninitialized value in join or string at arraydef.pl line 17. " The script is : 1 #!/usr/bin/perl 2 3 use strict; 4 5 my @a = ( 1...10 ); 6 7 print " Original array : @a... (5 Replies)
Discussion started by: praveen_b744
5 Replies
Login or Register to Ask a Question