Solved

Truncate last 40 chars of every line in a file

Posted on 1998-12-02
3
214 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now