Visit Our UNIX and Linux User Community


Sort a file content with space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort a file content with space
# 1  
Old 03-17-2014
RedHat Sort a file content with space

Hello,
I need help on.

I have a File which stores the information as below.
It is space separated file, I want to keep only unique record in file based on file name.
Also if you notice sometime filename with space appear in last column like (abc_ xyz1_bc12_20140312_c.xlsx)


Code:
03/17/2014  10:35 AM  618 Admin\vick       abc_ xyz1_bc12_20140312_c.xlsx
03/17/2014  10:35 AM  618 Admin\david     abc_xyz1_bc12_20140312_c.xls
03/17/2014  10:38 AM  618 Admin\samar    pqr_tbd1_bc12_20140312_c.doc
03/17/2014  10:34 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip
03/17/2014  10:39 AM  618 Admin\vick      abc_ xyz1_bc12_20140312_c.xlsx
03/17/2014  10:42 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip

Thank you for you help.
# 2  
Old 03-17-2014
Hello,

Following may help you.


Code:
awk '!a[$6]++' file_name


Output will be as follows.

Code:
03/17/2014  10:35 AM  618 Admin\vick       abc_ xyz1_bc12_20140312_c.xlsx
03/17/2014  10:35 AM  618 Admin\david     abc_xyz1_bc12_20140312_c.xls
03/17/2014  10:38 AM  618 Admin\samar    pqr_tbd1_bc12_20140312_c.doc
03/17/2014  10:34 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-17-2014
Code:
awk '{fname = "";
 for(i = 6; i <= NF; i++)
  {fname = (fname == "") ? $i : (fname OFS $i)};
 if(a[fname]++ == 0) {print $0}}'

This User Gave Thanks to SriniShoo For This Post:
# 4  
Old 03-17-2014
RedHat Sort the file content with Space

Quote:
Originally Posted by RavinderSingh13
Hello,

Following may help you.


Code:
awk '!a[$6]++' file_name


Output will be as follows.

Code:
03/17/2014  10:35 AM  618 Admin\vick       abc_ xyz1_bc12_20140312_c.xlsx
03/17/2014  10:35 AM  618 Admin\david     abc_xyz1_bc12_20140312_c.xls
03/17/2014  10:38 AM  618 Admin\samar    pqr_tbd1_bc12_20140312_c.doc
03/17/2014  10:34 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip


Thanks,
R. Singh

Thanks It works fine. I need to redirect the output to new file.
# 5  
Old 03-17-2014
Hello,

Just use > redirect operator for same at the end of solution as follows.

Code:
awk '!a[$6]++' file_name > OUTPUT_file_name


Thanks,
R. Singh
# 6  
Old 03-17-2014
Code:
akshay@Aix:/tmp$ cat file
03/17/2014  10:35 AM  618 Admin\vick       abc_ xyz1_bc12_20140312_c.xlsx
03/17/2014  10:35 AM  618 Admin\david     abc_xyz1_bc12_20140312_c.xls
03/17/2014  10:38 AM  618 Admin\samar    pqr_tbd1_bc12_20140312_c.doc
03/17/2014  10:34 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip
03/17/2014  10:39 AM  618 Admin\vick      abc_ xyz1_bc12_20140312_c.xlsx
03/17/2014  10:42 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip

akshay@Aix:/tmp$ awk  'match($0,/[^ ]* [^ ]*$/) && !x[substr($0,RSTART,RLENGTH)]++' file
03/17/2014  10:35 AM  618 Admin\vick       abc_ xyz1_bc12_20140312_c.xlsx
03/17/2014  10:35 AM  618 Admin\david     abc_xyz1_bc12_20140312_c.xls
03/17/2014  10:38 AM  618 Admin\samar    pqr_tbd1_bc12_20140312_c.doc
03/17/2014  10:34 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip

I think Ravinder you did not notice input properly

see below carefully

Code:
akshay@Aix:/tmp$ cat f
03/17/2014  10:35 AM  618 Admin\vick       abc_ xyz2_bc12_20140312_c.xlsx
03/17/2014  10:35 AM  618 Admin\david     abc_xyz1_bc12_20140312_c.xls
03/17/2014  10:38 AM  618 Admin\samar    pqr_tbd1_bc12_20140312_c.doc
03/17/2014  10:34 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip
03/17/2014  10:39 AM  618 Admin\vick      abc_ xyz1_bc12_20140312_c.xlsx
03/17/2014  10:42 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip

akshay@Aix:/tmp$ awk '!a[$6]++' f
03/17/2014  10:35 AM  618 Admin\vick       abc_ xyz2_bc12_20140312_c.xlsx
03/17/2014  10:35 AM  618 Admin\david     abc_xyz1_bc12_20140312_c.xls
03/17/2014  10:38 AM  618 Admin\samar    pqr_tbd1_bc12_20140312_c.doc
03/17/2014  10:34 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip

akshay@Aix:/tmp$ awk  'match($0,/[^ ]* [^ ]*$/) && !x[substr($0,RSTART,RLENGTH)]++' f
03/17/2014  10:35 AM  618 Admin\vick       abc_ xyz2_bc12_20140312_c.xlsx
03/17/2014  10:35 AM  618 Admin\david     abc_xyz1_bc12_20140312_c.xls
03/17/2014  10:38 AM  618 Admin\samar    pqr_tbd1_bc12_20140312_c.doc
03/17/2014  10:34 AM  618 Admin\titlis      pqr_tbd1_bc12_20140312_c.zip
03/17/2014  10:39 AM  618 Admin\vick      abc_ xyz1_bc12_20140312_c.xlsx

akshay@Aix:/tmp$ awk  '{match($0,/[^ ]* [^ ]*$/);print substr($0,RSTART,RLENGTH)}' f
abc_ xyz2_bc12_20140312_c.xlsx
 abc_xyz1_bc12_20140312_c.xls
 pqr_tbd1_bc12_20140312_c.doc
 pqr_tbd1_bc12_20140312_c.zip
abc_ xyz1_bc12_20140312_c.xlsx
 pqr_tbd1_bc12_20140312_c.zip

akshay@Aix:/tmp$ awk '{print $6}' f
abc_
abc_xyz1_bc12_20140312_c.xls
pqr_tbd1_bc12_20140312_c.doc
pqr_tbd1_bc12_20140312_c.zip
abc_
pqr_tbd1_bc12_20140312_c.zip


Last edited by Akshay Hegde; 03-17-2014 at 08:44 AM..
# 7  
Old 03-17-2014
In my opinion, Instead of complicating the code to deal with space, i would go for renaming the file without space.

Previous Thread | Next Thread
Test Your Knowledge in Computers #975
Difficulty: Easy
In July 2009, Alan Cox quit his Linux kernel development role as the TTY layer maintainer after disagreement with Linus Torvalds about who should pay for beers during a trip to Amsterdam.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Modify a file content in UNIX and sort for only required fields ?

I have the below contents in a file after making the below curl call curl ... | grep -E "state|Rno" | paste -sd',\n' | grep "Disconnected" > test "state" : "Disconnected",, "Rno" : "5554f1d2" "state" : "Disconnected",, "Rno" : "10587563" "state" : "Disconnected",, "Rno" :... (2 Replies)
Discussion started by: Vaibhav H
2 Replies

2. UNIX for Dummies Questions & Answers

How to sort a content of a text file using a shell script?

I am new to shell scripting. I am interested how to know how to sort a content of a file using shell scripting. I've attached the 'Input file' and the 'expected output' to this thread. Details provided in the expected output file will provide details on how the sort needs to be done. ... (16 Replies)
Discussion started by: nkarthik_mnnit
16 Replies

3. Shell Programming and Scripting

Replace file name with Space as content

Hi, I am having a files in my directory like this: 2014 1049_file1.txt 2014 1050_file2.txt 2014 1110_file3.txt 2014 1145_file4.txt 2014 2049_file5.txt I need to replace the above file names like this without changing the content of filename: file1.txt file2.txt file3.txt... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

4. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

Sort a file content using one column

Hello All, I have a file which have content as below. 03/09/2014 10:35 AM 618 Admin\rick pqr_ klm2_pog12_20140309_c.xlsx 03/10/2014 10:35 AM 618 user\test01 mplz_ fgh2_lal12_20140310_c.xlsx 03/17/2014 10:35 AM 618 Admin\vick abc_ xyz2_bc12_20140317_c.xlsx 03/18/2014 ... (2 Replies)
Discussion started by: kumar30213
2 Replies

6. Shell Programming and Scripting

Grep empty space and sort

Hi Expert, Kindly request for your expertise in this matter. I have below output: 12.125.124.173,xx1.common.com 12.125.124.174,xx2.common.com 12.125.124.175,xx3.common.com 12.125.124.176, 12.125.124.177, 12.125.124.178, 12.125.124.179,xx4.common.com 12.125.124.180,xx5.common.com... (8 Replies)
Discussion started by: regmaster
8 Replies

7. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

8. Shell Programming and Scripting

Sort content of text file based on date?

I now have a 230,000+ lines long text file formatted in segments like this: Is there a way to sort this file to have everything in chronological order, based on the date and time in the text? In this example, I would like the result to be: (19 Replies)
Discussion started by: KidCactus
19 Replies

9. UNIX for Dummies Questions & Answers

sort and find duplicates for files with no white space

example data 5666700842511TAfmoham03151008075205999900000001000001000++ 5666700843130MAfmoham03151008142606056667008390315100005001 6666666663130MAfmoham03151008142606056667008390315100005001 I'd like to sort on position 10-14 where the characters are eq "130MA". Then based on positions... (0 Replies)
Discussion started by: mmarshall
0 Replies

10. Shell Programming and Scripting

remove space from file content

i am a bit new to shell scripting i have a file containing xxxx xx xx but i want to output the content as xxxxxxxx. thus removing the space. any idea how i can do this (4 Replies)
Discussion started by: blackzinga
4 Replies

Featured Tech Videos