Solved

Truncate last 40 chars of every line in a file

Posted on 1998-12-02
3
217 Views
Last Modified: 2010-04-21
I am wondering if there is some function to turncate the last 40 chars of every line in a given file. And also, when I execute the system command like "system(". .profile");" in a C program, the profile is not executed.
0
Comment
Question by:bujji120198
3 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 30 total points
ID: 2008191
awk '{l=length($0);if(l>40){print substr($0,1,l-40)}}' file

How do you proove that .profile was not executed?
I suggest adding following to your .profile:
echo "called at `date` by $user" > /tmp.profile

Then tell us what the error is, or what does not work.
0
 
LVL 1

Expert Comment

by:toTo
ID: 2008192
another suggestion

"system(". ./.profile");"
0
 
LVL 3

Expert Comment

by:mliberi
ID: 2008193
about "truncating the first 40 characters":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do you need a C or shell function?

if C you can use strcpy()
if shell I suggest to use the 'typeset' internal command.

Example (ksh):
let's suppose we want to get the first 40 chars from var 'line'

typeset -L40 L40=$line

echo "$L40"   # first 40 chars, eventually padded with blanks

about "system(". .profile")":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I agree with ahoffmann, your .profile should get executed, but
if your purpose was to define and/or export variables, functions,
aliases and so on for sure you failed!

In fact when you call the 'system' C library function your process forks a shell that executes the command ". .profile". All the variables you define there remains in the subprocess environment and are lost when the subprocess exit, just before the 'system' function returns.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What are recommended OS for exim mail server? 10 109
Unix Question 19 50
Unix Command -- Challenging  question 7 89
Searching File Names and Eliminating Duplicates in Linux 4 68
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question