How to take every number in in file by sed?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to take every number in in file by sed?
# 1  
Old 04-20-2015
Wrench How to take every number in in file by sed?

Hi all..,
I have problem when i want to take every number in my file.

this is my file look like..

errorpath.csv

Code:
error=29
error=34
error=2
error=3
error=6
error=5
error=36
error=36
error=36
error=36
error=36
error=36
error=36
error=36
error=36
error=36

from this file i want to take every different number.

Actually that file was generated by myprogram. I can make the result just a number.
for example

errorPath.csv
Code:
32
32
4
4
4
4
36
2
2
2
2
2
2
2
2
2
2

I think I can solve my task if I can make new file by get data from errorPath.csv by make other file like this.
this is file that i expect can maked.

result.cfgvar1=32
var2=4
var3=36
var4=2
I need shell script to that task and I think that SED command can help me.
Thanks before..

Last edited by weslyarfan; 04-20-2015 at 11:20 AM.. Reason: Tell more detail
# 2  
Old 04-20-2015
That is a strange name for a file that contains no commas, but, assuming your sample data is representative of your real data, the following should do what you want:
Code:
awk '!A[$0]++' errorpath.csv

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.

Last edited by Don Cragun; 04-20-2015 at 02:25 AM.. Reason: Add standard Solaris awk note.
# 3  
Old 04-20-2015
If it's just for the number, modify Don Cragun's proposal slightly:
Code:
awk '!A[$2]++ {print $2}' FS="=" errorpath.csv

# 4  
Old 04-20-2015
Can I do it by SED command?
# 5  
Old 04-20-2015
I'm afraid, no. sed doesn't have variables to keep all the duplicate values.
# 6  
Old 04-20-2015
Quote:
Originally Posted by weslyarfan
Can I do it by SED command?
I don't know if you can do it or not. Editors (including ed, ex, and sed) are not designed to perform logical evaluations like this. I can imagine using one sed script to extract the numbers found in your file and using a shell while read loop to feed each of those numbers to another sed script that could then be used delete duplicate occurrences of that number. But, invoking sed N + 1 times for an N line file (instead of invoking awk once) would be extremely inefficient (and would not be just using sed.
# 7  
Old 04-20-2015
I think nice idea if I can take the value and put its value to a variabel in one other file.
But totally I dont know how to make it real..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File manipulation place 0 before the number using sed

I'm new with sed, and i am really confused with slashes, backslashes, parentheses, I've tried reading some beginner's guide, but still trouble fixing this problem, do you have any tips where or what to read to learn more about sed? can you also help me with my problem? Note: I was tasked to use... (4 Replies)
Discussion started by: akopocpoypoy
4 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line... (4 Replies)
Discussion started by: sudeep.id
4 Replies

4. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

5. UNIX for Dummies Questions & Answers

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line to... (1 Reply)
Discussion started by: sudeep.id
1 Replies

6. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

7. Shell Programming and Scripting

Extract a number from a line in a file and sed in another copied file

Dear all, I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far: for ((k=1;k<4;k++)); do temp=`sed -n "${k}p"... (2 Replies)
Discussion started by: mnaqvi
2 Replies

8. Shell Programming and Scripting

how to change a number in a file with sed?

Hello, I have a file which contains some line as follows, 9.9 TEMP 9.9 MUCOEFF 0.0 EPSILON And I want to increase this MUCOEFF by 0.2 as 9.9 TEMP 10.1 MUCOEFF 0.0 ... (7 Replies)
Discussion started by: lorenzz
7 Replies

9. UNIX for Dummies Questions & Answers

how to number format a data file without using SED?

Hi I have a file which contains data (list of data) and I want to put a number with bracket 1) 2) 3) etc at the beginning of every successive line I can do it with SED and I can also do it using the nl route but am looking for a different method. I'm guessing I would need some sort of loop... (3 Replies)
Discussion started by: Cactus Jack
3 Replies

10. Shell Programming and Scripting

SED: Update Last Line with Number Lines in File

Hi, I have to update last line of a text file with the number of lines in that file. This last line will have text such as 0.0000 and I should replace this with number lines. If lines are 20 then it should be replaced with 00020. Any sed or awk cmd help would be appreciated (3 Replies)
Discussion started by: bmkux
3 Replies
Login or Register to Ask a Question