Remove comma and next rows beginning from the end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove comma and next rows beginning from the end
# 1  
Old 11-04-2010
Remove comma and next rows beginning from the end

Hello friends,

I have a file which consists of many rows, I use a couple of commands to convert it so i can use in a database query for filtering. I need the first columns (msisdns) in a row, seperated with commas,
Code:
9855162267,4,5,2010-11-03 17:02:07.627
9594567938f,5,5,2010-11-02 12:47:08.047
9855155486,4,5,2010-11-01 12:26:37.640
9233453445f,5,5,2010-11-02 11:20:43.327
9434326423,5,5,2010-11-01 11:02:02.217
9592416210f,4,5,2010-11-02 10:20:52.063

Code:
nawk -F, '{print $1}' FILE | sed -e 's/f$//g' -e 's/\([0-9]\{10\}\)/91\1/g' | nawk '!_[$0]++' | nawk -v RS="\n" -v ORS=","  '{}'1

code works well but in output there is a comma at the end and more, so i cant save it to a file. I know it is easier to get rid of it using "print" options but i couldnt, I appreciate any suggestion to remove the colored part, is it also possible without adding another command with pipe?

Code:
919855162267,919594567938,919855155486,919233453445,919434326423,919592416210,server{root}/a/b/c>

Regards
# 2  
Old 11-04-2010
This should do it in 1 nawk command:

Code:
nawk -F, '{ printf (NR==1?"":",")$1} END {printf "\n"}' FILE


Last edited by Chubler_XL; 11-04-2010 at 10:21 PM.. Reason: Fix error
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 11-05-2010
To drop the f force $1 into numerical context:
Code:
nawk -F, '{printf (NR>1?FS:x)91$1+0} END{print x}' infile


Last edited by Scrutinizer; 11-05-2010 at 06:54 AM.. Reason: Forgot 91 at the start (thanx rdcwayx)
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 11-05-2010
Code:
awk -F, '{gsub(/f/,"",$1)}NR==1{a="91" $1;b[$1]++;next}!b[$1]++ {a=a ",91" $1}END{print a}' FILE

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 11-05-2010
Quote:
Originally Posted by Scrutinizer
To drop the f force $1 into numerical context:
Code:
nawk -F, '{printf (NR>1?FS:x)91$1+0} END{print x}' infile

@Scruti

with the +0 then i get a result in scientific notation :
Code:
# awk -F, '{printf (NR>1?FS:x)91$1+0} END{print x}' infile
919.85516e+09,919.59457e+09,919.85516e+09,919.23345e+09,919.43433e+09,919.59242e+09

# 6  
Old 11-05-2010
That is probably because your awk cannot handle large integers. Try:
Code:
nawk -F, '{printf (NR>1?FS:x)"91%.0f",$1} END{print x}' infile

(which is better practice anyway Smilie . Another advantage is there is no need for +0)
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-05-2010
Yep ! that one is better Smilie

x stand for an empty string , correct ?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to append in the beginning not at the end?

Hi, I now that >> will append text to the end of the text that is already inside the file. How to append the new text infront of the text that is already in the file. Thanks for any input. Regards, Chandu (3 Replies)
Discussion started by: chandrakanth
3 Replies

2. Shell Programming and Scripting

How can we remove comma from end of each line ?

Hi, How can we remove the comma from the end of each line. I have a csv file in below format. file.csv Name,age,gender,location, Joel,18,M,Newyork, Monoj,21,M,Japan, Litu,23,M,turki, Expected o/p file1.csv Name,age,gender,location (4 Replies)
Discussion started by: Litu19
4 Replies

3. Shell Programming and Scripting

Removing hyphen from beginning and end of a word only.

It is very simple to remove a hyphen from a word anywhere in that word using a simple sed command (sed -i 's/-//g' filename), but I am not able to figure out how to do this: For example, apple -orange tree pipe- banana-shake dupe- What my output should look like: apple orange tree... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

4. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

5. UNIX for Dummies Questions & Answers

Regex for beginning of line until a comma

What is a regex for "the dalai lama, his holiness the" that would just grab "the dalai lama" and one that would just grab "his holiness the"? Both should exclude the comma.. I was trying '^.*' and many variants with no luck. (6 Replies)
Discussion started by: glev2005
6 Replies

6. Shell Programming and Scripting

How to remove white spaces from the beginning an end of a string in unix?

Suppose, I have a variable var=" name is ". I want to remove the blank spaces from the begining and endonly, not from the entire string. So, that the variable/string looks like following var="name is". Please look after the issue. (3 Replies)
Discussion started by: mady135
3 Replies

7. Shell Programming and Scripting

Awk: Remove comma at the end of the string

Hi i had String like UID: ABC345QWE678GFK345SA90, LENGTH 32 when I used awk ' FS, {print $1}' prints ABC345QWE678GFK345SA90, how can i getrid of that coma at the end of the string. Thanks in advance.. (14 Replies)
Discussion started by: Reddy482
14 Replies

8. Shell Programming and Scripting

Command filtering ONLY rows NOT beginning with '*'

I need a command which filters rows ONLY NOT beginning with '*' So far I have following NOT sufficient command, because it does not include ALL possible literals except of '*' grep ^ INPUT_FILE >>OUTPUT_FILE Is it possible to write something like grep NOT ^ INPUT_FILE... (3 Replies)
Discussion started by: ABE2202
3 Replies

9. UNIX for Dummies Questions & Answers

cat a file from end to beginning

Is there an option, for cat, head, tail, or is there any way, to display a file from last line to first? For example, my file looks like this: aaaa bbbb cccc eeee and I would like to print or display it like this: eeee cccc bbbb aaaa thanks (5 Replies)
Discussion started by: jpprial
5 Replies
Login or Register to Ask a Question

Featured Tech Videos