Allignment

Hi All,

I want to shift the last and second last line to 8 spaces in the right inside a file. please can somebody suggest a script for the same.

Thanks and Best Regards,
Shazin

tab size is generally 8 spaces.

you can try something like..

awk -v last_minus_2=$(cat file | wc -l) '{if(NR>(last_minus_2 - 2)) printf "\t%s\n", $0; else print $0 }' file

Hi Anchal,

Many thanks, also please can you describe how this command will work as I have never worked with awk.

Best Regards,
Shazin

---------- Post updated at 12:58 PM ---------- Previous update was at 12:39 PM ----------

Hi Anchal,

Also it is giving Syntax error. Please can you help.

Best Regards,
Shazin

The logic is quite simple,

treat "last_minus_2" as "total_records" ( i have given that name with some other logic in my mind and forgot to change it.)

so,

awk -v total_records=$(cat file | wc -l) '{if(NR>(total_records - 2)) printf "\t%s\n", $0; else print $0 }' file

now,

-v total_records=$(cat file | wc -l)

define the awk variable "total_records" that contains the total number of records ( total lines) in the file.

'{if(NR>(total_records - 2)) printf "\t%s\n", $0; else print $0 }' 

NR is an awk system variable which denotes the number of the current record.
so, if that number is "last" or "second last" then append a "\t".

update:

what error are you getting?

Hi Anchal,

Many Thanks.... Got the logic perfectly..... :slight_smile:

I am getting the below error:

Error
awk: syntax error near line 1
awk: bailing out near line 1

can you post exactly what you typed?

---------- Post updated at 01:41 PM ---------- Previous update was at 01:40 PM ----------

also which OS?

Hi Anchal,

I typed the below code:

Code:
awk -v total_records = $( cat redirects.virgin-atlantic.com.conf | wc -l ) '{ if ( NR > ( total_records - 2 ) ) printf "\t%s\n", $0 ; else print $0 }' redirects.virgin-atlantic.com.conf

I am using the below version:
SunOS 5.8 Generic_108528-23 sun4u sparc SUNW,Sun-Fire-280R

Many Thanks,
Shazin

you didn't use code tags hence I couldn't figure out the syntax possibilities.

anyways,

if you are using solaris, then try /usr/xpg4/bin/awk or nawk

and check if it works.

Hi Anchal,

I used nawk and was getting the below error:

nawk: syntax error at source line 1
context is
>>> = <<<
nawk: bailing out at source line 1

Thanks,
Shazin

do you have spaces around the "=" ??

awk -v something=something

if yes, please remove.

Hi Anchal,

That was the problem, but now am getting the below error.

nawk: can't open file {if (NR >(total_records - 2)) printf "\t%s\n", $0; else print $0}
source line number 1

Best Regards,
Shazin

I still doubt you have space.
please double check that there is no space in anyside of "=".

check and if fails, post the command with code tags.

Please find the code that I am using:

Code:
nawk -v total_records=$(cat redirects.virgin-atlantic.com.conf | wc -l) '{if(NR>(total_records - 2)) printf "\t%s\n", $0; else print $0}' redirects.virgin-atlantic.com.conf

---------- Post updated at 02:19 PM ---------- Previous update was at 02:18 PM ----------

nawk -v total_records=$(cat redirects.virgin-atlantic.com.conf | wc -l) '{if(NR>(total_records - 2)) printf "\t%s\n", $0; else print $0}' redirects.virgin-atlantic.com.conf

it is working with me.

$bash$:-> awk -v total_records=$(cat redirects.virgin-atlantic.com.conf | wc -l) '{if(NR>(total_records - 2)) printf "\t%s\n", $0; else print $0}' redirects.virgin-atlantic.com.conf
ceName: Node-1
processName: tzMgmt
Status: PROCESS_NOT_RUNNING
ceName: Node-2
processName: tzMgmt
Status: PROCESS_RUNNING
ceName: Node-1
processName: XDM
Status: PROCESS_NOT_RUNNING
ceName: Node-2
        processName: XDM
        Status: PROCESS_RUNNING

Hi Anchal,

Can this be done by using sed command.

Best Regards,
Shazin

---------- Post updated at 06:18 PM ---------- Previous update was at 02:39 PM ----------

Hi All,

Its giving me the below error.

syntax error: `(' unexpected

Best Regards,
Shazin

---------- Post updated at 06:33 PM ---------- Previous update was at 06:18 PM ----------

Hi All,

I think I have found a workaround for the same.

sed '$s/^/        /' redirects.virgin-atlantic.com.conf > temp

[/CODE]

won't this update only last line?

Hi Anchal,

Yes I am appending the file Line by Line so this will do for me. Many thanks for all your efforts I think this was not working because of the version of the Unix installed on my system.

Best Regards,
Shazin

Perheaps and old version of shell.
Try to replace the $(..) form by `...`:

awk -v total_records=`cat redirects.virgin-atlantic.com.conf | wc -l` '{if(NR>(total_records - 2)) printf "\t%s\n", $0; else print $0}' redirects.virgin-atlantic.com.conf

Jean-Pierre.

Hi

Many thanks. but I have formulated a work around for the same....

Cheers,
Shazin

No Ok:
awk -v total_records = $(

Ok:
awk -v total_records=$(

You have to delete the blank spaces