Squares in saved code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Squares in saved code
# 1  
Old 03-26-2016
Squares in saved code

can you help

i am merging 2 files together and saving to a third file with awk
and its working with this code

Code:
awk 'OFS="";NR==FNR{a[FNR]=$0;next} {print a[FNR],"\n","\b",$0}' file1 file2 > file3

the problem is in file3 when its saved
i get a small square at the start of every 2nd line (see picture)

i need to get rid of the squares
whats wrong with the code ?

thanks


File 1
======
Code:
AAAA
BBBB
CCCC

File 2
======
Code:
XXXX
YYYY
ZZZZ

File 3
======
Code:
AAAA
XXXX
BBBB
YYYY
CCCC
ZZZZ


Last edited by Scrutinizer; 03-26-2016 at 07:12 PM.. Reason: code tags
# 2  
Old 03-26-2016
Why are you printing a backspace character (\b) ? Just leave it out.



--
As a matter of efficiency, set OFS="" in the BEGIN section...
In fact, you do not need it at all if your use print a[FNR] "\n" $0

Last edited by Scrutinizer; 03-26-2016 at 07:16 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-26-2016
@Scrutinizer

i have removed \b from the code

Code:
awk 'OFS="";NR==FNR{a[FNR]=$0;next} {print a[FNR],"\n",$0}' file1 file2 > file3

and the squares have gone

thank you
# 4  
Old 03-27-2016
This is exactly what paste is for:
Code:
paste -d"\n" file[12]
AAAA
XXXX
BBBB
YYYY
CCCC
ZZZZ

This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-27-2016
Quote:
Originally Posted by bob123
@Scrutinizer

i have removed \b from the code

Code:
awk 'OFS="";NR==FNR{a[FNR]=$0;next} {print a[FNR],"\n",$0}' file1 file2 > file3

and the squares have gone

thank you
If you go with awk instead of RudiC's suggestion of paste you can still simplify your code (as Scrutinizer suggested) and get exactly the same output:
Code:
awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR] "\n" $0}' file1 file2 > file3

(Note that the commas in your print statement are the only things that use the value of OFS in your script.)
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 03-27-2016
Or
Code:
awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR]} 1' file1 file2 > file3

This User Gave Thanks to RudiC For This Post:
# 7  
Old 03-27-2016
Or:
Code:
awk '1;getline<f>0' f=file2 file1 > file3

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Squares on a chessboard calculation

Hi All, Just curious if the following formula is possible within a shell script: n x (n + 1) x (2n + 1) ______________________ 6 so far im just using a simple expression but need to implement the above. Many thanks in advance #!/bin/sh echo "\n" echo -------- Squares... (5 Replies)
Discussion started by: sammclean23
5 Replies

2. UNIX for Dummies Questions & Answers

Script Not getting Saved

Hi , Script File Is Not Getting Saved This Are The Steps I Am Following For Saving And Executing A Script 1). vi ( To Open Vi Editor ) 2). vi filename ( vi firstprog.ksh) #!bin\kash date 3) !wq :( Saving And Quit) When I Am Saving The Scrpit I Am Getting The Below... (1 Reply)
Discussion started by: anudeepkumar123
1 Replies

3. UNIX for Dummies Questions & Answers

where alias saved?

step 1 # alias alias cp='cp -i' alias l.='ls -d .* --color=tty' alias ll='ls -l --color=tty' alias ls='ls --color=tty' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' step 2 # cat ~/.bashrc # .bashrc (3 Replies)
Discussion started by: cqlouis
3 Replies

4. UNIX and Linux Applications

Bluefish: where are the preferences saved?

I have just tried out Bluefish as an alternative to my regular text editor. If I save the modified preferences and reboot, the preferences have to be reentered again. Does anyone know which file the preferences are saved in? The command find / -mmin -5 | grep bluefish yields zero hits. Thanks... (2 Replies)
Discussion started by: figaro
2 Replies

5. Shell Programming and Scripting

How can I identify the last saved log?

Our system produce logs when a script is run which may not be daily, the logs have a format: name_YYMMDD.log - both name and .log are consistent, date changes as per the day the script is run. Is there a way of finding the last saved log? (20 Replies)
Discussion started by: gugs
20 Replies

6. Shell Programming and Scripting

Bash squares and exponents

I'm trying to write a simple bash script and I need something like var=2^(5+i) where i is another variable. How would do I this in bash? (1 Reply)
Discussion started by: jeriryan87
1 Replies

7. UNIX for Advanced & Expert Users

Sudo file not saved

Hi all, I have edited my sudoers file. I am using visudo command I have added the following lines and saved the file. I am saving the lines as :wq But I am very amazed to see that these lines are not written in the sudoers file. I have retried the above process many times, when I... (0 Replies)
Discussion started by: Asteroid
0 Replies

8. UNIX for Advanced & Expert Users

only root's crontab gets not saved

Hi, Something funny is happening over here: when a regular user edits his cron-file (crontab -e) saves and exits vi the correct new cron-file gets installed and saved to disk. But if root does the same, vi saves it but if I then check the cron-file it has the previous contents! I did strace (==... (1 Reply)
Discussion started by: flok
1 Replies
Login or Register to Ask a Question