Print only one occurrence of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print only one occurrence of a file
# 1  
Old 11-15-2014
Print only one occurrence of a file

I have the below file

Code:
$cat sample.txt
one.zip#one.pdf#one.jpeg#1
two.zip#two.pdf#two.jpeg#1
two.zip#two.pdf#two.jpeg,three.jpeg#2
two.zip#two.pdf#two.jpeg,three.jpeg,four.jpeg#3
three.zip#three.pdf#three.jpeg#1


I need the output as

Code:
one.zip#one.pdf#one.jpeg#1
two.zip#two.pdf#two.jpeg,three.jpeg,four.jpeg#3
three.zip#three.pdf#three.jpeg#1

the logic is the file .zip should have only one entry and that should be the largest file name and I will not know the number of lines or no of files.
It is an automated script that will produce the file.

Please help.

Last edited by Don Cragun; 11-15-2014 at 03:37 AM.. Reason: Add CODE tags.
# 2  
Old 11-15-2014
Welcome to the forum.

Any efforts on your part?
# 3  
Old 11-15-2014
Try This..

Code:
awk -F"#" '{ arr[$1]=$0;}
END{ for(v in arr) print arr[v]}'  sample.txt

Output

Code:
three.zip#three.pdf#three.jpeg#1
one.zip#one.pdf#one.jpeg#1
two.zip#two.pdf#two.jpeg,three.jpeg,four.jpeg#3

This User Gave Thanks to bharat1211 For This Post:
# 4  
Old 11-15-2014
@Bhavi

You'll find out that bharat1211 post does not produce what you want.
Code:
arr[$1]=$0;

will overwrite the previous entry without regard if it has more files or less.

Please, let us know what you have tried.

Last edited by Aia; 11-15-2014 at 02:57 AM.. Reason: Adding why it will not work.
# 5  
Old 11-15-2014
Yes it is working for my requirement.


Thank you,
# 6  
Old 11-15-2014
Then run it on this input:
Code:
one.zip#one.pdf#one.jpeg#1
two.zip#two.pdf#two.jpeg,three.jpeg,four.jpeg#3
two.zip#two.pdf#two.jpeg,three.jpeg#2
two.zip#two.pdf#two.jpeg#1
three.zip#three.pdf#three.jpeg#1

# 7  
Old 11-15-2014
My previous requirement is satisfied and its working fine now.

Now i need another help for the below condition.

I have the below file

$cat sample.txt
Code:
one.zip#one.pdf#one.jpeg#1
two.zip#two.pdf#two.jpeg#1
two.zip#two.pdf#three.jpeg#1
two.zip#two.pdf#four.jpeg#
three.zip#three.pdf#three.jpeg#1

The file should look like below with all the jpeg files for the zip file in the same line.

Code:
one.zip#one.pdf#one.jpeg#1
two.zip#two.pdf#two.jpeg,three.jpeg,four.jpeg#3  --> count of jpeg files
three.zip#three.pdf#three.jpeg#1

I googled and found the below script

Code:
awk -F "," 's != $1 || NR ==1{s=$1;if(p){print p};p=$0;next}
{sub($1,"",$0);p=p""$0;}END{print p}' sample.txt

But its again printing the same. Please advice


Thanks,

Moderator's Comments:
Mod Comment Please use CODE tags when showing sample input, output, and code.
Please do not use one thread for two different problems; open a new thread instead of adding on to an existing thread.

Last edited by Don Cragun; 11-15-2014 at 01:45 PM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

awk to extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

4. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

5. Shell Programming and Scripting

print only the first occurrence of a pattern

Hi, I have a file as below select or create proc /*comments*/ /*comments*/ /*comments*/ /*comments*/ ( variables4 datatypes1, variables1 datatypes2, variables2 datatypes3, variables3 datatypes2 ) some text some text ( sometext some text ) some text some text (3 Replies)
Discussion started by: manasa_vs
3 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

8. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

9. Shell Programming and Scripting

Sed to print a string until the second occurrence of a character

Hi, I am totally new to shell scripting. I have a String "c:\working\html\index.txt.12-12-2009.bkp" I want to check if the string has more than one "." character. If it does I would like to retrieve only "c:\working\html\index.txt" i.e, discard the second occurrence of "." and the rest of the... (7 Replies)
Discussion started by: imr
7 Replies

10. Shell Programming and Scripting

Print last occurrence if first field match

Hi All, I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me... (9 Replies)
Discussion started by: Raynon
9 Replies
Login or Register to Ask a Question