Using sed (or similar) to rename variable headings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed (or similar) to rename variable headings
# 1  
Old 04-24-2009
Using sed (or similar) to rename variable headings

Hello,

I'm rather new to the world of regular expressions and sed, though am excited by its possibilities. I have a particular task I'd like to achieve, and have googled the topic quite a bit. However, having found some codes that perform a task very similar to what I'd like to do, I can't for the life of me fathom how to adapt them. So I figured I'd try asking someone with a bit more experience.

Basically I have a dataset in a text file and I want to change the variable headings.

Currently they read:

Code:
p_1 p_2 p_3 ... p_19

and I'd like to change them to:

Code:
P[,1] P[,2] P[,3] ... P[,19]

Is this something that can be done using sed?
Thanks awfully!
# 2  
Old 04-24-2009
Try this:

Code:
sed 's/_\([0-9][0-9]*\)/[,\1]/g' filename


cheers,
Devaraj Takhellambam
# 3  
Old 04-28-2009
That's great - thanks very much!

Ideally I'd like to change the letters in the variable names from lower case to upper case. I've managed to do this by piping several sed commands together:

Code:
cat input.txt | sed 's/_\([0-9][0-9]*\)/[,\1]/g' | sed 's/p/P/g' | sed 's/d/D/g' >> output.txt

(there are only p's and d's in the variable names)

Out of curiosity (and because I'd like to try to learn more about the sed command), is there a way of achieving this with just one sed command?

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get a printer name into variable with similar printers

I am using a same printer vendor for 2 printer. Some of them dont have just default name with a full name of that printer. for example # lpstat -a | awk '{print $1}'|grep CITIZEN CITIZEN CITIZEN-T-S851II This first one CITIZEN is a part of script if grep -q S310II /etc/cups/printers.conf;... (10 Replies)
Discussion started by: tomislav91
10 Replies

2. Shell Programming and Scripting

Isolate text with sed or similar utility

All, I'm getting a list like the following and I'd like to kill each PID in turn. pid (17797) pid (21748) pid (21754) pid (21704) pid (2199) pid (2159) pid (17809) pid (21769) pid (21778) pid (21715) ... (3 Replies)
Discussion started by: ejianu
3 Replies

3. Answers to Frequently Asked Questions

Why Parsing Can't be Done With sed ( or similar tools)

Regularly we have questions like: i have an XML (C, C++, ...) file with this or that property and i want to extract the content of this or that tag (function, ...). How do i do it in sed? Yes, in some (very limited) cases this is possible, but in general this can't be done. That is: you can do... (0 Replies)
Discussion started by: bakunin
0 Replies

4. Shell Programming and Scripting

Editing files with sed or something similar

{ "AFafa": "FAFA","AFafa": "FAFA" "baseball":"soccer","wrestling":"dancing" "rhinos":"crocodiles","roles":"foodchain" } I need to insert a new line before the closing brackets "}" so that the final output looks like this: { "AFafa": "FAFA","AFafa": "FAFA"... (6 Replies)
Discussion started by: SkySmart
6 Replies

5. Shell Programming and Scripting

To find ls of similar pattern files in a directory by passing the variable.

Hi, I have file in my $datadir as below :- SAT_1.txt SAT_2.txt BAT_UD.lst BAT_DD1.lst DUTT_1.txt DUTT_la.txt Expected result :- should get all the above file in $<Filename>_file.lst Below is my code :- for i in SAT BAT DUTT do touch a.lst cd $datadir (1 Reply)
Discussion started by: satishmallidi
1 Replies

6. Shell Programming and Scripting

Perl match multiple numbers from a variable similar to egrep

I want to match the number exactly from the variable which has multiple numbers seperated by pipe symbol similar to search in egrep.below is the code which i tried #!/usr/bin/perl my $searchnum = $ARGV; my $num = "148|1|0|256"; print $num; if ($searchnum =~ /$num/) { print "found"; }... (2 Replies)
Discussion started by: kar_333
2 Replies

7. Shell Programming and Scripting

Remove the headings

HI I am executing faloowing commands. mr batch_1 > my_temp.txt ; mr batch_2 >>my_temp.txt; mr batch_3 >> my_temp.txt; mr batch_4 >> my_temp.txt; and the out put file is as this cat my_temp.txt Machine Name Max Load Current Load Factor O/S Status... (3 Replies)
Discussion started by: ptappeta
3 Replies

8. Shell Programming and Scripting

awk, sed or similar log repair help

I have a log file that for some reason, once or two time a month, line foods are missing. This log is generated from vmstat everyminute. I dont know why sometimes it does this. Each line in the log should have 18 columns separated by one or more spaces. Good Log: (not actual log) 1 1... (8 Replies)
Discussion started by: Ikon
8 Replies

9. UNIX for Dummies Questions & Answers

ls -l column headings

I'm trying to see if there's a way to see column headings when I type the ls -l command. I know what some of the fields mean for example in the following listing: total 136 drwxr-xr-x 2 root root 4096 Jun 5 15:16 bin drwxr-xr-x 3 root root 4096 Jul 9 15:25 boot drwxr-xr-x 9... (6 Replies)
Discussion started by: thoughts
6 Replies

10. Shell Programming and Scripting

sed or awk to convert text files with recurring headings to rows and colum

I have many text file reports generated by a Information Assurance tool that I need to get into a .CSV format or Excel tab delimited format. I want to use sed or awk to grab all the information in the sample text file below and create column headings:Risk ID, Risk Level, Category, Description, How... (5 Replies)
Discussion started by: Bjoeboo
5 Replies
Login or Register to Ask a Question