Login or Register to Ask a Question and Join Our Community


To search distinct special char in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To search distinct special char in file
# 1  
Old 09-03-2013
To search distinct special char in file

hi,

i need to search distinct special character from a file which is a pipe delimited from a specific column

for eg:
Code:
input file.txt
aa|bb|cc|$abc
aa|bb|ccc|#abol
bb|xss|ddd|$xyz
nn|yyy|qqq|=qqqq
abe|qqq|yyy|=aaa
aaa|yyy|zzzz|#aaaa
.
.
.


my desired output
$
#
=

i know which column will have the special character for eg here col is 4th column
# 2  
Old 09-03-2013
Which distinct characters are you looking for...
# 3  
Old 09-03-2013
I think below command give you idea.

Code:
cut -f4 -d'|' filename | cut -c1

For uniqness.

Code:
cut -f4 -d'|' filename | cut -c1 | sort -u


Last edited by learnbash; 09-03-2013 at 03:42 PM.. Reason: added code
# 4  
Old 09-03-2013
If what you want is the 1st occurrence of the 1st character in field 4 in your file, the following should be a little more efficient than learnbash's suggestion:
Code:
awk -F'|' '!((c = substr($4,1,1)) in s) {s[c];print c}' file.txt

Otherwise, as shamrock said, we need to know what you mean by special, where in field 4 the special character can appear, and whether there can be more than one special character in field 4 in any line in your input file.

If you want to try the above awk script on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
# 5  
Old 09-03-2013
Hi,

i would like to take the distinct special char in 4th field where in it can occur in 4th filed any where
Code:
for my fourth fieldeg:
$aab$c#
$$abc.c=
.
.
.

output should be distinct special char like (!@#$%^&*()_",:Smilie other than numbers and characters
Code:
$#.=

# 6  
Old 09-03-2013
Try this for non alnum chars in field 4:
Code:
sed -r 's/([^|]*\|){3}//;s/[[:alnum:]]*$//' file
$
#
$
=
=
#

EDIT: Or, if any position in field 4 is possible:
Code:
sed -r 's/([^|]*\|){3}//;s/^[[:alnum:]]*//;s/[[:alnum:]]*$//' file
$
#
$
=
=
#

# 7  
Old 09-03-2013
Like Don's sample, this runs on | separated field #4,
but this sample deletes the characters in the character set [a-zA-Z0-9]
Code:
awk -F"|" '{gsub("[a-zA-Z0-9]","",$4); print $4}' file

It is also possible to reverse the character set by a leading ^ followed by the special characters:
Code:
awk -F"|" '{gsub("[^-=!@#$%^&*()_]","",$4); print $4}' file

NB a - in the character set should be first, otherwise it would be interpreted as a range...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To search null and char in file

Hi guys, I want to know how i can search null value and character value in a feed file. my file has Pipe delimted records. for eg: 1|12|xxx|yyy . . . N records I know the the which column null and char occurs does it has for eg 3rd column which i can obtain by awk $3 and (7 Replies)
Discussion started by: rohit_shinez
7 Replies

2. Programming

Special char in a buffer

Hello, Please ,how can verify, if a case of buffer contains a special char ? (in C) Thank you. (3 Replies)
Discussion started by: chercheur857
3 Replies

3. Shell Programming and Scripting

Split a special char

Hello, I have some data in output file.In that i need to split the special char "(" and ")" and store it. This is example of o/p file. (OHC12345) (OHC12415) (OHC12765) (OHC12545) I need like OHC12345 OHC12415 OHC12765 OHC12545 --Thanks (5 Replies)
Discussion started by: rasingraj
5 Replies

4. Shell Programming and Scripting

Search distinct files

Hello , Can anyone help me with my below query I am trying to find a text in directory of files via below command grep -i cmps_cgs_crs_rfnc_id * But it returns multiple times same file name i.e if the text found in a file 4 times the file name shown 4 times in the o/p Is... (1 Reply)
Discussion started by: Pratik4891
1 Replies

5. Shell Programming and Scripting

Remove special char from end of the file

Hi I am working on a bash script and would know how to use cut or sed to remove (F/.M/d h) from a text file. Before 1 text to save (F/.M/d h) after 1 text to save Thanks in advance (5 Replies)
Discussion started by: pelle
5 Replies

6. Shell Programming and Scripting

Special Char in Data file

Hi All, I have a data file in UNIX which i am trying to load into Oracle table using Oracle SQL Loader. The problem is, one of the filed contains special character (ex: Square). And due to this reason, my script fails. Could you please let me know, how to identify which character is... (1 Reply)
Discussion started by: Amit.Sagpariya
1 Replies

7. UNIX for Dummies Questions & Answers

How to copy/move to a file with a special character as the 1st char in the filename?

I am trying to create files with special characters in its filenames for testing purposes. This is on a Linux RHEL4 but this should also be applicable on a Unix shell. I am able to create files with special characters in the filenames...e.g. cp -pv foo.gif \*special.gif cp -pv foo.gif \... (6 Replies)
Discussion started by: sqa777
6 Replies

8. Shell Programming and Scripting

Special Char in Multiple Files

We develop a file in windows and move to unix box as a part of deployment. When we do this, we get ctrl-M(^M) character added to the file. So we need to remove ctrl-M(^M) character from all the files from deployment folder and all subfolders folder. Currently we move to individual folders and... (5 Replies)
Discussion started by: thinakarmani
5 Replies

9. UNIX for Advanced & Expert Users

Using egrep to search for Text and special char

Anyone is well-versed to use egrep to search a file for a line containing both: 1) AAA 2) $ I am having problem escaping the dollar sign when using egrep in conjunction with satisfying AAA as well. E.g. Text file Line 1 AAA Line 2 $$$ Line 3 AAA BBB $ Line 4 $$$ BBB AA will return me... (2 Replies)
Discussion started by: izy100
2 Replies

10. UNIX for Dummies Questions & Answers

search special characters in a file

Hello I am new to shell scripting and can anyone tell me how to check if there are any special characters in a file. Can i use grep ? thanks susie (2 Replies)
Discussion started by: cramya80
2 Replies
Login or Register to Ask a Question

Featured Tech Videos