last value of a field, put it on one line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting last value of a field, put it on one line?
# 1  
Old 03-19-2009
last value of a field, put it on one line?

I have a text file like the following:

Major=1
Minor=2
Sub=3
Path=4

It is a text file with a full version number of some software. Im trying to get the 1234 to show up just like that on one line.

Ive tried the following:

cat file.txt | awk -F"=" '{print $2}' | sed -e s/\\r\$/\./g (and various different escape options)

&

cat file.txt | awk -F"=" '{print $2}' | tr -d "\r" and all various forms as well...

Any one care to point out how to get this to print out the way I want? end result id like to see is 1.2.3.4, but I can live with just 1234.
# 2  
Old 03-19-2009
Quote:
Originally Posted by trey85stang
I have a text file like the following:

Major=1
Minor=2
Sub=3
Path=4

It is a text file with a full version number of some software. Im trying to get the 1234 to show up just like that on one line.

Ive tried the following:

cat file.txt | awk -F"=" '{print $2}' | sed -e s/\\r\$/\./g (and various different escape options)

&

cat file.txt | awk -F"=" '{print $2}' | tr -d "\r" and all various forms as well...

Any one care to point out how to get this to print out the way I want? end result id like to see is 1.2.3.4, but I can live with just 1234.
Try this:

Code:
awk -F= '{printf("%s%s",NR==1?"":".", $2)}END{print}' file

BTW, the use of cat with awk or sed is redundant.

Regards
# 3  
Old 03-19-2009
Code:
$ cat info.txt
Major=1
Minor=2
Sub=3
Path=4
$ . ./info.txt
$ echo $Major.$Minor.$Sub.$Path
1.2.3.4
$

# 4  
Old 03-19-2009
Quote:
Originally Posted by Franklin52
Try this:

Code:
awk -F= '{printf("%s%s",NR==1?"":".", $2)}END{print}' file

BTW, the use of cat with awk or sed is redundant.

Regards
thanks for the help, I realize cat/sed (even awk) is redundant.. but its just habit when I start trying to break something down like this.

what is the {printf("%s%s",NR==1?"":".", $2)} actually dong here? Im a bit confused? btw this works great.. just trying to learn what I am doing right here Smilie
# 5  
Old 03-19-2009
Hi,

This will do ..


Code:
$ cat out.lst
Major=1
Minor=2
Sub=3
Path=4

Code:
$ awk -F"=" '{print $2}' out.lst|tr '\n' '.'|sed 's/.$/\n/g'
1.2.3.4

Thanks
Sha
# 6  
Old 03-19-2009
Quote:
Originally Posted by trey85stang
what is the {printf("%s%s",NR==1?"":".", $2)} actually dong here? Im a bit confused? btw this works great.. just trying to learn what I am doing right here Smilie
The printf statement prints two strings, a dot and field 2 but the dot shouldn't print with the first line.
The conditional statement checks for the linenumber NR, if it is the 1st line it prints nothing ("") before the 2nd field otherwise it prints a dot (".") before the the 2nd field.

Regards
# 7  
Old 03-19-2009
Code:
awk -F"=" '{print $2}' ORS="" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

2. UNIX for Dummies Questions & Answers

Put a character before each line

i have few lines: 3 4 5 6 #72 9 9 9 4 5 67 8 9 #4 6 8 92 1i want to put '#' before each line. i used this awk '{print "#" $0;}it gave #3 4 5 6 ##72 9 9 9 #4 5 67 8 9 ##4 6 8 92 1but i want the output like this: #3 4 5 6 #72 9 9 9 #4 5 67 8 9 #4 6 8 92 1I want to avoid the character... (5 Replies)
Discussion started by: arindam guha
5 Replies

3. Shell Programming and Scripting

Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level. For Example, File 1: A,B,C,D,E,F 1,2,3,4,5,6 File 2: A,C,B,D,E,F 1,2,4,5,5,6 out put file: (12 Replies)
Discussion started by: karingulanagara
12 Replies

4. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

6. Programming

Perl - how to put to the next line if I have same pattern in one line

Dear All, Could you help me how to put to the next line if I have pattern below in Perl language. Data-123 Linux MacOSData-124 windows FreeBSDData-125 OpenBSD NetBSD I would the output below: Data-123 Linux MacOS Data-124 windows FreeBSD Data-125 OpenBSD NetBSD ... (2 Replies)
Discussion started by: askari
2 Replies

7. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

8. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

9. Shell Programming and Scripting

Cut data and put it in next line

here is my sample file dn: cn=Anandmohan Singh,ou=addressbook,dc=thbs,dc=com objectClass: top objectClass: person objectClass: organizationalPerson objectClass: inetOrgPerson cn: Anandmohan Singh givenName: Anandmohan mail: anand_ms@thbs.com mobile: 9986010455 ou: null... (16 Replies)
Discussion started by: namishtiwari
16 Replies

10. Shell Programming and Scripting

AWK line by line instead of field by field?

I've been using a lot of awk lately for csv files. But I've been using awk for csv files that contain 32 fields per line. For the first time, I've been given a csv file that contains one field per line (13 fields in each csv file). I need to check that a specific field, or line contains a... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question