SH Script help. editing string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SH Script help. editing string
# 1  
Old 10-10-2008
SH Script help. editing string

I have a string that looks like this

username|field1|field2|field3

the data has a delimiter of "|"
how can i edit field1, keeping the rest of the data the same
also how can i edit field2 and 3.
# 2  
Old 10-10-2008
edit in the sense you want to replace the first field with some other word keeping other intact??
if so use awk
Code:
echo "username|field1|field2|field3"|awk -F\| '{print "word|"$2"|"$3"|"$4}'

output will be
word|field1|field2|field3
same way you can do your second question also
# 3  
Old 10-10-2008
> edit in the sense you want to replace the first field with some other word keeping other intact??
yup that was what i meant



that is very simple to understand.
thank you very much
# 4  
Old 10-10-2008

No need for any external commands:

Code:
string="username|field1|field2|field3"
newfield1=something
newstring="$newfield1|${string#*|}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help regarding String editing

Hi Geeks I am working on trimming the logs and extracting the XMLs from it. I am facing one problem here. My XML String is ending with ...........Request></Body></Envelope>S/R sometimes there is more then just S/R in the end. I want to delete anything comes after </Envelope>... (3 Replies)
Discussion started by: santy00110011
3 Replies

2. Shell Programming and Scripting

Editing part of the string

Hi guys got a problem here hope u all can help me. I learn that sed can actually edit a string but you need to know the old attribute to change to new 1. Example: sed "s/$title:$author/$title:$Nauthor/g" "Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50" Each delimiter : represent... (4 Replies)
Discussion started by: GQiang
4 Replies

3. UNIX for Dummies Questions & Answers

Help with editing string elements

Hi All I have a question. I would like to edit some string characters by replacing with characters of choice located in another file. For example in sample file>S5_SK1.chr01 NNNNNNNNNNNNNNNNNNNCAGCATGCAATAAGGTGACATAGATATACCCACACACCACACCCTAACACTAACCCTAATCTAACCCTGGCCAACCTGTTT... (9 Replies)
Discussion started by: pawannoel
9 Replies

4. UNIX for Dummies Questions & Answers

Making a script for editing

Hello all. I am trying to make a script to edit any file by replacing a string with a string. The script should be able to be applied to any given file. for instance it should be able to replace "foo" with "bar" in the file myFile.txt. I know i need to make a back up file for this to work. I just... (2 Replies)
Discussion started by: iwatk003
2 Replies

5. Homework & Coursework Questions

String editing using sed? awk?

1. The problem statement, all variables and given/known data: Problem Statement for project: When an account is created on the CS Unix network, a public html directory is created in the account's home directory. A default web page is put into that directory. Some users replace or... (13 Replies)
Discussion started by: peage1475
13 Replies

6. Solaris

editing files with script

hi guys, We have to implement new local (/etc/default/login) USER security policy on almost 50 stations. so editing /etc/default/login and /etc/default/passwd will be way too long work. Can we do the same using some script, I mean editing the above files and putting variables as RETRIES=3, ... (5 Replies)
Discussion started by: Asteroid
5 Replies

7. Programming

Editing or Adding to ELF string tables

Is it possible to add - or edit - data (the strings) contained within the string table of an ELF executable? I know I can access the string table with the following code; while ((scn = elf_nextscn(m_elf, scn)) != 0) { char *name = 0; gelf_getshdr(scn, &shdr); ... (9 Replies)
Discussion started by: Dhodder
9 Replies

8. Shell Programming and Scripting

string editing in files

Hi all, I'm fairly new to scripting in linux and need some help. I have an file that looks something like this: ~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Some comments # Some comments # Some comments # Some comments # Some comments # Some comments abc:/path/to/somewhere:X... (3 Replies)
Discussion started by: Avatar Gixxer
3 Replies

9. Shell Programming and Scripting

Editing a file using a script

Hi I have about 10 config files belonging to software that runs on SCO UNIX. These files contain, amongst many other things, a path which points to the software locations. We normally have to change them manually every time the software is coppied to a new location and when you gotta do a few... (45 Replies)
Discussion started by: Ypnos
45 Replies

10. UNIX for Dummies Questions & Answers

Editing one string in multiple files

I am trying to edit multiple files from one directory and including all the files in all the sub directories. My string opens each file, puts the text on my screen and does not save the new information to the file. I am using a variable in my script, and wondering if that is what is choking it. ... (1 Reply)
Discussion started by: Skoshi
1 Replies
Login or Register to Ask a Question