Place variables at the beginning of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Place variables at the beginning of each line
# 1  
Old 11-15-2010
Place variables at the beginning of each line

Hello all,

I am very new to the shell scripting and I hope someone can help me with this.
I have thousands of files with certain format of information and I need to do this for all my files.

For each file, grab the numbers in the first and second rows and place them in the position 1 and 2 of the rest rows.
The numbers of the first and second rows are different for each file and also each file has different number of rows.

Here's the examples:

Code:
ptrfghbc 44.23
mkhtwsbhp 234.51
1995 56 26 26358 266665 251 32
1995 65 58 22355 551223 352 25
1996 32 52 55852 255632 335 25

I'd like to have something like this:

Code:
ptrfghbc 44.23
mkhtwsbhp 234.51
44.23 234.51 1995 56 26 26358 266665 251 32
44.23 234.51 1995 65 58 22355 551223 352 25
44.23 234.51 1996 32 52 55852 255632 335 25

Your help is much appreciated.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-15-2010 at 06:01 PM.. Reason: code tags, please!
# 2  
Old 11-15-2010
Franklin52's solution below should solve the problem

Last edited by danmero; 11-15-2010 at 06:15 PM.. Reason: Out of scope.
# 3  
Old 11-15-2010
Code:
sed '
  N
  p
  s/.* \(.*\)\n.* \(.*\)/\1 \2/
  :loop
  $d
  N
  s/\n/ /
  p
  s/\( [^ ]*\) .*/\1/
  b loop
 '

Narrative: When the first line is in the buffer, get the second, print both, extract the final fields of both and concatenate to be prefix, create a loop branch target, delete the prefix if there are no more lines, get the next line, turn the linefeed into a space, print it out, remove the suffix and loop.

Last edited by DGPickett; 11-15-2010 at 03:58 PM.. Reason: Put and preserve space in prefix.
# 4  
Old 11-15-2010
Another one:
Code:
awk '/^[a-z]/{s=s?s FS $2:$2; print; next}{$1=s FS $1}1' file

# 5  
Old 11-15-2010
Code:
awk '(NR<3){A[NR]=$2}{print((NR>2)?A[1]" "A[2]" "$0:$0)}' input

or
Code:
awk '(NR<3){A[NR]=$2}{print((NR>2)?A[1]FS A[2]FS$0:$0)}' input

or
Code:
awk '{A[NR]=$2;print(NR>2)?A[1]FS A[2]FS$0:$0}' input

Code:
[root@lyisy22:~/sand]# cat input
ptrfghbc 44.23
mkhtwsbhp 234.51
1995 56 26 26358 266665 251 32
1995 65 58 22355 551223 352 25
1996 32 52 55852 255632 335 25
[root@lyisy22:~/sand]# awk '(NR<3){A[NR]=$2}{print((NR>2)?A[1]" "A[2]" "$0:$0)}' input
ptrfghbc 44.23
mkhtwsbhp 234.51
44.23 234.51 1995 56 26 26358 266665 251 32
44.23 234.51 1995 65 58 22355 551223 352 25
44.23 234.51 1996 32 52 55852 255632 335 25
[root@lyisy22:~/sand]#


Last edited by ctsgnb; 11-15-2010 at 03:44 PM..
This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 11-15-2010
Thank you so very much everybody.

All the codes suggested by ctsgnb are working flawlessly. Great work!

I tested the two codes by danmero and Franklin52 and nothing is changed in my file. I got no error but it just returned the original file back to me.

I have not tested the code by DGPickett yet. I am a newbie and I need more time to figure out what should I do with these lines! I will surely test it later today.

You guys are awesome. Thanks again!

Last edited by GoldenFire; 11-15-2010 at 05:31 PM..
# 7  
Old 11-15-2010
Code:
awk '(NR<3&&p=p (p?FS:x)$2)||$0=p FS$0' file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Remove new line for a particular place

Hello All, I have a text file which gets uploaded to tables using shells script. However before running that script I need to alter it, like in the below I have to firstly find the word 1234 and remove the new line from end of it. 1234,5678,fasfasasfsadf abc changes to... (11 Replies)
Discussion started by: Sandeep_sandy
11 Replies

2. Shell Programming and Scripting

beginning less from line #

Hi from a script i want to to read a file beginning at line e.g. number 21 to the EOF. less +n21 temp.txt Bevor the result, it brings an empty page, so that i cant use for scripting. Any idea how the problem can be solved? Thanks in advance! IMPe (2 Replies)
Discussion started by: IMPe
2 Replies

3. Shell Programming and Scripting

comment a line of the patterns is a the beginning of the line

I need to comment the lines starting with pattern "exclude" or "exclude=". If the work exclude comes at any other part, ignore it. Also, ignore, excludes, excluded etc. Ie only comment the line starting with exclude. File contents. exclude exclude= hi I am excluded excludes excludes= ... (9 Replies)
Discussion started by: anil510
9 Replies

4. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

5. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

6. Shell Programming and Scripting

Space at beginning of the line

How can I delete spaces at the begining of all lines of my file ? (2 Replies)
Discussion started by: Sara_84
2 Replies

7. Shell Programming and Scripting

Using NR with two variables at the beginning of awk

hi My requirement is this: I have a file having around 100000 records pipe delimited. Now I want to compare record 1 with record 2 and similarly record3 with record 4, this goes on.. For this purpose i put a script as follows: #!bin/ksh ct_line=1 nxt_line=`expr ${ct_line} + 1` awk -F "|"... (1 Reply)
Discussion started by: ramkrix
1 Replies

8. Shell Programming and Scripting

SED command help: Can we pass predefined variables in place of regex

Hi All, I have a doubt. Can we assign a regular expression for pattern searching to a variable in a script and then use that variable in place of a regular expression in sed command.I tried but got some syntax error!!Is it not possible.Because my requirement is that i have a generic script to get... (8 Replies)
Discussion started by: usha rao
8 Replies

9. Shell Programming and Scripting

Getting the value of a line, that changes place

Hi I am trying to get the value of several results in a file called seq032.diag. The values I am looking for is down under Smooth Tracking nodes and is for g01r01 g02r01 s01t02 etc etc. The problem is that when I try to use look for text and tail etc, it works fine in one result file. In... (1 Reply)
Discussion started by: Navigatorchief
1 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question