Relative column offsets


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Relative column offsets
# 1  
Old 05-02-2013
Relative column offsets

Ive been trying to slide the second column one line down while keeping the first one in place. For a start, I tried to see if I can go through every line and print the element located on the line below with the following command, but it didnt work.
Maybe awk is not the right way to do it. Matlab can do this easily, but if there is a way to do this in unix, I'd highly appreciate it. thank you!

awk '{for(i=1; i<NR; i++) FNR==i {print i, $(i+1)}}' filename.txt
# 2  
Old 05-02-2013
It would help to supply some input, using code tags, and show your expected output. Smilie
# 3  
Old 05-02-2013
Okay, trying. thank you
Input:
7 1
49 0
83 0
5 2
42 0

Hopeful output:
7 0
49 0
83 0
5 2
42 0
# 4  
Old 05-02-2013
Quote:
Originally Posted by Alabama
Okay, trying. thank you
Input:
7 1
49 0
83 0
5 2
42 0

Hopeful output:
7 0
49 0
83 0
5 2
42 0
I don't get it. You talked about changing the next line down, but all you have done here is change the 1 in the 2nd field of the 1st line to a zero. Nothing else changed??? You said you wanted to keep the first one in place, but the first 1 is the only thing that you changed???
# 5  
Old 05-02-2013
In your output you only replaced the 2nd field in the 1st line Smilie
From your text I understand output should be
7 0
49 1
83 0
5 0
42 2

That would be
Code:
awk '{print $1+0,save+0} {save=$2}'

# 6  
Old 05-02-2013
Indeed, this is what I meant. Fantastic, this worked! Thank you.
# 7  
Old 05-03-2013
FYI, the awk code is executed left to right for each line(row).
A loop can hardly span over many lines (unless readline is used to "read ahead" but this often gets ugly).
Here the variable save stores the field2 at the current line, to be printed at the next line. (Initially save is "" or 0, casted to integer 0 by adding 0. Another method is to initialize with BEGIN {save=0}).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Relative Pathnames

Hi, Could anyone help me with the following questions about relative addressing. The questions are: 1) Enter below the command to copy the file basics.pptto the folder outline, using relative addressing. 2) Enter below to move the file .secrets.doc to the folder Day1 using relative addressing.... (1 Reply)
Discussion started by: ml123
1 Replies

2. Shell Programming and Scripting

Relative path not safe

Hallo everyone, I am running an SQL-Script over KShell-Script. Thus, in the KShell-Script there are these lines: WORKPATH=$PWD/work EXPORTDIR=export_meine_datei_bitte EXPORTPATH=${WORKPATH}/${EXPORTDIR} ... db2 connect to ${DBNAME} || die "can not open database connection" db2... (9 Replies)
Discussion started by: ratnalein88
9 Replies

3. Shell Programming and Scripting

Converting date/time and generating offsets in bash script

Hi all, I need a script to do some date/time conversion. It should take as an input a particular time. It should then generates a series of offsets, in both hour:minute form and number of milliseconds elapsed. For 03:00, for example, it should give back 04:02:07 (3727000ms*) 05:04:14... (2 Replies)
Discussion started by: emdan
2 Replies

4. UNIX for Dummies Questions & Answers

Absolute and Relative Paths?

Can someone cofirm that I have got the paths correct here? :confused: $PATH_TO_TMP_DIR='/tmp'; #$PATH_TO_TMP_DIR='home/tmp'; $PATH_TO_YOUR_IMG_DIR = '/temp_images'; #$PATH_TO_YOUR_IMG_DIR = 'home/public_html/Midwich/temp_images'; Thanks (1 Reply)
Discussion started by: stubie
1 Replies

5. Shell Programming and Scripting

how to read the relative path

suppose i ahve a shell script Nsdnet.sh inside a directory /dialp/Release/bin another file nsdnet_file.csv is under the same directory. Now in the shell script i have call a java file, which reads the csvfile from the commandline. Now when i run the file as $ ./Nsdnet.sh ./nsdnet_file.csv then... (5 Replies)
Discussion started by: priyanka3006
5 Replies

6. UNIX for Dummies Questions & Answers

problem with relative pathing

Hi all, My directory structure is as follows home /md/DEV/SCRIPTS/DAILY and home/md/DEV/MIS/LANDING so this command find home/md/DEV/MIS/LANDING -name MIS_Customer_\* is giving me the desired output ... (1 Reply)
Discussion started by: rajarp
1 Replies

7. UNIX for Dummies Questions & Answers

get cygpath to leave relative paths as relative?

If I execute mypath=`cygpath -w ../` echo $mypath I get d:\unix\nextVersion\script OK, d:\unix\nextVersion\script is the correct windows version of the path, but it is in absolute form. I would prefer it if cygpath left it in relative form, i.e. echo $mypath should output ..\ ... (0 Replies)
Discussion started by: fabulous2
0 Replies

8. UNIX for Dummies Questions & Answers

list all ports and their relative IP@ if any

Hi all i'm working on a LINUX-based platform. i'm little confused with PORTs. i have my platform connected to many other platforms, i need to know the relative port for each IP@. i know the IP of each connected platform to mine, but i'm not sure about the relative PORT for each platform...... (4 Replies)
Discussion started by: samsal_991
4 Replies

9. Programming

relative pointers on Unix

Hi all: We're porting lot of C code from Windows to Unix. In Windows we're using relative pointers (with the _based keyword) to access some structures placed on shared memory. We would need something like the Microsoft's _based keyword for unix. Does something similar exist in Unix? If not, is... (3 Replies)
Discussion started by: rahul_verma
3 Replies

10. Shell Programming and Scripting

Relative directory - NOT absolute

Here is the drill, I am using a script to login to a remote ftp, and put and get files. My question is: I want to login and automatically change to the same directory I am in on my machine. I can not use $home, pwd or anyother env variable (that I know) since the names of the machines are totally... (4 Replies)
Discussion started by: sierra_aar
4 Replies
Login or Register to Ask a Question