Copying x words from end of line to specific location in same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying x words from end of line to specific location in same line
# 1  
Old 02-18-2010
Copying x words from end of line to specific location in same line

Hello all
i know it is pretty hard one but you will manage it all

after noticing and calculating i find a rhythm for the file i want to edit
to copy the last 12 characters in line but the problem is to add after first 25 characters in same line

in other way too copy the last 12 characters in line and add them after /up/upfiles/ in same line

the problem i faced mainly how to copy word from end of the line to the beginning of line

i am still newbie by the way

here is the input:

Code:
Redirect 303 /up/upfiles/  /http://img189.imageshack.us/img189/3162/3jy83426.jpg
Redirect 303 /up/upfiles/  /http://img696.imageshack.us/img696/5284/3kr88432.jpg
Redirect 303 /up/upfiles/  /http://img411.imageshack.us/img411/918/3nw51001.jpg

i want the output to be like this :

Code:
Redirect 303 /up/upfiles/3jy83426.jpg  /http://img189.imageshack.us/img189/3162/3jy83426.jpg
Redirect 303 /up/upfiles/3kr88432.jpg  /http://img696.imageshack.us/img696/5284/3kr88432.jpg
Redirect 303 /up/upfiles/3nw51001.jpg  /http://img411.imageshack.us/img411/918/3nw51001.jpg

Thanks in advance
# 2  
Old 02-18-2010
Try this:
Code:
awk '{n=split($NF,a,"/");$3=$3 a[n]}1' file

# 3  
Old 02-18-2010
Code:
sed 's|\(  *[^ ]*/\)\([^/]*\)$|\2\1\2|' infile


Last edited by radoulov; 02-18-2010 at 10:17 AM.. Reason: Refactored.
# 4  
Old 02-18-2010
use below:-

Code:
nawk '{match($NF,/[^\/]*.jpg/);$3=$3substr($NF,RSTART)}1' file.txt

SmilieSmilieSmilie
# 5  
Old 02-18-2010
Or:

Code:
awk -F/ '$4=$NF$4' OFS=/ infile

# 6  
Old 02-18-2010
hello
i will try now all of them and reply to each one
thanks for you all

---------- Post updated at 04:42 PM ---------- Previous update was at 04:32 PM ----------

Quote:
Originally Posted by Franklin52
Try this:
Code:
awk '{n=split($NF,a,"/");$3=$3 a[n]}1' file

Worked really really fine

Quote:
Originally Posted by radoulov
Code:
sed 's|\(  *[^ ]*/\)\([^/]*\)$|\2\1\2|' infile

Worked really really fine

Quote:
Originally Posted by ahmad.diab
use below:-

Code:
nawk '{match($NF,/[^\/]*.jpg/);$3=$3substr($NF,RSTART)}1' file.txt

SmilieSmilieSmilie
Worked really really fine but i can't use it because the file is about 4 thousands lines and not all .jpg

Quote:
Originally Posted by radoulov
Or:

Code:
awk -F/ '$4=$NF$4' OFS=/ infile

Worked really really fine

i really appreciate your help guys with me
and i really want to learn this language
what is the best way to learn it ?
# 7  
Old 02-18-2010
Quote:
Originally Posted by princesasa

Worked really really fine but i can't use it because the file is about 4 thousands lines and not all .jpg
use modified code below:-

Code:
nawk '{match($NF,/[^\/]*$/) ; $3=$3substr($NF,RSTART)}1' infile.txt

SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

2. Shell Programming and Scripting

Bash: copying lines with specific character to files with same name as copied line.

I am trying to make my script as simple as a possible but, I am not sure if the way I am approaching is necessarily the most efficient or effective it can be. What I am mainly trying to fix is a for loop to remove a string from the specified files and within this loop I am trying to copy the lines... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

3. UNIX for Dummies Questions & Answers

How to return only specific words in a line?

Hi, I'm a bash newbie and have a data set like this idxxx1 something something marker_id_132=rsxxx;marker_id_135=rsxxx idxxx2 something something marker_id_132=rsxxx;marker_id_135=rsxxx idxxx3 something something marker_id_132=rsxxx;marker_id_135=rsxxx ... ... (7 Replies)
Discussion started by: hanhel
7 Replies

4. Shell Programming and Scripting

Insert text line to specific location CSV

In Perl. ***edited question below*** Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file: age,name,locationIs it possible to INSERT data into the CSV into the correct age order. For example, if I had... (1 Reply)
Discussion started by: whyte_rhyno
1 Replies

5. Shell Programming and Scripting

Interchange location of words in a line of csv file

Hi, I have a file with some info as example below. Its a csv file and i want to interchange the location of the times and dates in the 3rd and 4th fields only. I am using SunOS and csh script and would like to incorporate sed or nawk in the script (or any that would be able to accomplish this).... (5 Replies)
Discussion started by: viper1503
5 Replies

6. Shell Programming and Scripting

copying to the end of the line

Hi, I have a file as such 1 <text1><text2></ 2 <text3><text4></ 3 <text5><text6></ 4 <text7><text8></ I need is so the first bit of text in the line is at the end as its xml so 1 <text1><text2></text1> 2 <text3><text4></text3> 3 <text5><text6></text5> 4 ... (12 Replies)
Discussion started by: legolad
12 Replies

7. Shell Programming and Scripting

Extract X words from end of line, minus last keynumber X

The file contains one line of text followed by a number. I want to take the number X at the end, take it out and display the last X words. X is the key telling me how many words from the end that I want and X will always be less than the number of words, so no problem there. Example input and... (4 Replies)
Discussion started by: fubaya
4 Replies

8. Shell Programming and Scripting

How to append a string to a specific location in a line

Hi, I have a to modify a line and insert a keyword in the middle to a specific location. My line looks like this FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (ABC, DEF, GHI) I want to change it as FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING... (4 Replies)
Discussion started by: mwrg
4 Replies

9. Shell Programming and Scripting

how to Add word at specific location in line

eg . i have file x.txt contains : java coding , shell scriptting etc... now i want to add "is langauge" after word java. output should be java is langauge coding , shell scriptting etc... any idea how to use shell script to do it ? (10 Replies)
Discussion started by: crackthehit007
10 Replies

10. Shell Programming and Scripting

How to append words at the end of first line

Hi Unix gurus This is my first post here. I have a file which looks like this: string1,string2 ,string3 ,string4 ... ... I need to append all words below first line to the first line, ie string1,string2,string3,string4,... Bear in mind that it is not known how many lines follow... (11 Replies)
Discussion started by: lmatlebyane
11 Replies
Login or Register to Ask a Question